Menu: Difference between revisions
Content added Content deleted
(Applesoft BASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 21: | Line 21: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">V items = [‘fee fie’, ‘huff and puff’, ‘mirror mirror’, ‘tick tock’] |
||
L |
L |
||
Line 33: | Line 33: | ||
I Int(reply) C 1..items.len |
I Int(reply) C 1..items.len |
||
print(‘You chose: ’items[Int(reply) - 1]) |
print(‘You chose: ’items[Int(reply) - 1]) |
||
L.break</ |
L.break</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 56: | Line 56: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">DEFINE PTR="CARD" |
||
BYTE FUNC Init(PTR ARRAY items) |
BYTE FUNC Init(PTR ARRAY items) |
||
Line 92: | Line 92: | ||
res=GetMenuItem(items,count) |
res=GetMenuItem(items,count) |
||
PrintF("You have chosen: %S%E",items(res)) |
PrintF("You have chosen: %S%E",items(res)) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Menu.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Menu.png Screenshot from Atari 8-bit computer] |
||
Line 112: | Line 112: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with ada.text_io,Ada.Strings.Unbounded; use ada.text_io, Ada.Strings.Unbounded; |
||
procedure menu is |
procedure menu is |
||
Line 138: | Line 138: | ||
put_line ("You chose " & |
put_line ("You chose " & |
||
choice ((+"fee fie",+"huff and puff",+"mirror mirror",+"tick tock"),"Enter your choice ")); |
choice ((+"fee fie",+"huff and puff",+"mirror mirror",+"tick tock"),"Enter your choice ")); |
||
end menu;</ |
end menu;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 147: | Line 147: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''}} |
||
< |
<syntaxhighlight lang="algol68">PROC menu select := (FLEX[]STRING items, UNION(STRING, VOID) prompt)STRING: |
||
( |
( |
||
INT choice; |
INT choice; |
||
Line 174: | Line 174: | ||
printf(($"You chose "g"."l$, menu select(items, prompt))) |
printf(($"You chose "g"."l$, menu select(items, prompt))) |
||
)</ |
)</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 186: | Line 186: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">menu: function [items][ |
||
selection: neg 1 |
selection: neg 1 |
||
while [not? in? selection 1..size items][ |
while [not? in? selection 1..size items][ |
||
Line 197: | Line 197: | ||
] |
] |
||
menu ["fee fie" "huff and puff" "mirror mirror" "tick tock"]</ |
menu ["fee fie" "huff and puff" "mirror mirror" "tick tock"]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 219: | Line 219: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">Menu(list:=""){ |
||
if !list ; if called with an empty list |
if !list ; if called with an empty list |
||
return ; return an empty string |
return ; return an empty string |
||
Line 228: | Line 228: | ||
InputBox , Choice, Please Select From Menu, % string ,, % 200<len*7 ? 200 ? len*7 , % 120 + x.count()*20 |
InputBox , Choice, Please Select From Menu, % string ,, % 200<len*7 ? 200 ? len*7 , % 120 + x.count()*20 |
||
return x[Choice] |
return x[Choice] |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">list = |
||
( |
( |
||
fee fie |
fee fie |
||
Line 238: | Line 238: | ||
MsgBox % Menu(list) ; call menu with list |
MsgBox % Menu(list) ; call menu with list |
||
MsgBox % Menu() ; call menu with empty list |
MsgBox % Menu() ; call menu with empty list |
||
return</ |
return</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f MENU.AWK |
# syntax: GAWK -f MENU.AWK |
||
BEGIN { |
BEGIN { |
||
Line 267: | Line 267: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Axe}}== |
=={{header|Axe}}== |
||
{{incorrect|Axe|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
{{incorrect|Axe|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
||
In Axe, static data (such as strings) is laid out sequentially in memory. So the H in "HUFF" is the byte after the null terminator for "FIE". However, null terminators are only added to strings when they are stored with the store symbol →. strGet returns a pointer to the start of the nth null-terminated string in the data, which is why the strings must be laid out in memory correctly. |
In Axe, static data (such as strings) is laid out sequentially in memory. So the H in "HUFF" is the byte after the null terminator for "FIE". However, null terminators are only added to strings when they are stored with the store symbol →. strGet returns a pointer to the start of the nth null-terminated string in the data, which is why the strings must be laid out in memory correctly. |
||
< |
<syntaxhighlight lang="axe">"FEE FIE"→Str1 |
||
"HUFF AND PUFF"→Str2 |
"HUFF AND PUFF"→Str2 |
||
"MIRROR MIRROR"→Str3 |
"MIRROR MIRROR"→Str3 |
||
Line 286: | Line 286: | ||
Return |
Return |
||
End |
End |
||
Disp strGet(Str1,N-1),i</ |
Disp strGet(Str1,N-1),i</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
< |
<syntaxhighlight lang="qbasic"> function sel$(choices$(), prompt$) |
||
if ubound(choices$) - lbound(choices$) = 0 then sel$ = "" |
if ubound(choices$) - lbound(choices$) = 0 then sel$ = "" |
||
ret$ = "" |
ret$ = "" |
||
Line 301: | Line 301: | ||
while ret$ = "" |
while ret$ = "" |
||
sel$ = ret$ |
sel$ = ret$ |
||
end function</ |
end function</syntaxhighlight> |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
While the following example could be lengthened to demonstrate larger menu-driven projects, it is useful to simply print the resulting string indexed by the user input. |
While the following example could be lengthened to demonstrate larger menu-driven projects, it is useful to simply print the resulting string indexed by the user input. |
||
< |
<syntaxhighlight lang="applessoftbasic"> 10 M$(4) = "TICK TOCK" |
||
20 M$(3) = "MIRROR MIRROR" |
20 M$(3) = "MIRROR MIRROR" |
||
30 M$(2) = "HUFF AND PUFF" |
30 M$(2) = "HUFF AND PUFF" |
||
Line 322: | Line 322: | ||
170 NEXT M |
170 NEXT M |
||
180 M$ = M$(N%) |
180 M$ = M$(N%) |
||
190 RETURN </ |
190 RETURN </syntaxhighlight> |
||
==={{header|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
Line 328: | Line 328: | ||
While the following example could be shortened to simply print the resulting string indexed by the user input, it is useful to demonstrate that larger menu-driven projects benefit from the use of the <code>ON n... GOSUB</code> statement to pass control to larger subroutines. |
While the following example could be shortened to simply print the resulting string indexed by the user input, it is useful to demonstrate that larger menu-driven projects benefit from the use of the <code>ON n... GOSUB</code> statement to pass control to larger subroutines. |
||
< |
<syntaxhighlight lang="commodorebasic">1 rem menu |
||
5 rem rosetta code |
5 rem rosetta code |
||
10 gosub 900 |
10 gosub 900 |
||
Line 379: | Line 379: | ||
1010 data "Mirror, mirror","You seem to be the fairest of them all!" |
1010 data "Mirror, mirror","You seem to be the fairest of them all!" |
||
1015 data "Tick tock","Time passes..." |
1015 data "Tick tock","Time passes..." |
||
1020 data "<Quit>","You decide to leave."</ |
1020 data "<Quit>","You decide to leave."</syntaxhighlight> |
||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
Example 1 |
Example 1 |
||
< |
<syntaxhighlight lang="dos">@echo off & setlocal enabledelayedexpansion |
||
set "menuChoices="fee fie","huff and puff","mirror mirror","tick tock"" |
set "menuChoices="fee fie","huff and puff","mirror mirror","tick tock"" |
||
Line 412: | Line 412: | ||
echo.Invalid Input. Please try again... |
echo.Invalid Input. Please try again... |
||
pause |
pause |
||
goto :tryagain</ |
goto :tryagain</syntaxhighlight> |
||
Example 2 |
Example 2 |
||
< |
<syntaxhighlight lang="dos"> |
||
@echo off |
@echo off |
||
Line 441: | Line 441: | ||
echo.!string[%choice%]! |
echo.!string[%choice%]! |
||
goto:eof |
goto:eof |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM list$(4) |
||
list$() = "fee fie", "huff and puff", "mirror mirror", "tick tock" |
list$() = "fee fie", "huff and puff", "mirror mirror", "tick tock" |
||
selected$ = FNmenu(list$(), "Please make a selection: ") |
selected$ = FNmenu(list$(), "Please make a selection: ") |
||
Line 464: | Line 464: | ||
IF index%>=0 IF index%<=DIM(list$() ,1) IF list$(index%)="" index% = -1 |
IF index%>=0 IF index%<=DIM(list$() ,1) IF list$(index%)="" index% = -1 |
||
UNTIL index%>=0 AND index%<=DIM(list$(), 1) |
UNTIL index%>=0 AND index%<=DIM(list$(), 1) |
||
= list$(index%)</ |
= list$(index%)</syntaxhighlight> |
||
Empty entries in the list are not offered as options, nor accepted as a selection. |
Empty entries in the list are not offered as options, nor accepted as a selection. |
||
=={{header|Brat}}== |
=={{header|Brat}}== |
||
< |
<syntaxhighlight lang="brat">menu = { prompt, choices | |
||
true? choices.empty? |
true? choices.empty? |
||
{ "" } |
{ "" } |
||
Line 488: | Line 488: | ||
} |
} |
||
p menu "Selection: " ["fee fie" "huff and puff" "mirror mirror" "tick tock"]</ |
p menu "Selection: " ["fee fie" "huff and puff" "mirror mirror" "tick tock"]</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 534: | Line 534: | ||
return items[choice - 1]; |
return items[choice - 1]; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp"> |
||
using System; |
using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 569: | Line 569: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <string> |
#include <string> |
||
#include <vector> |
#include <vector> |
||
Line 621: | Line 621: | ||
std::cout << "You chose: " << data_entry("> ", terms) << std::endl; |
std::cout << "You chose: " << data_entry("> ", terms) << std::endl; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
< |
<syntaxhighlight lang="ceylon">"Run the module `menu`." |
||
shared void run() { |
shared void run() { |
||
value selection = menu("fee fie", "huff And puff", "mirror mirror", "tick tock"); |
value selection = menu("fee fie", "huff And puff", "mirror mirror", "tick tock"); |
||
Line 647: | Line 647: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn menu [prompt choices] |
||
(if (empty? choices) |
(if (empty? choices) |
||
"" |
"" |
||
Line 671: | Line 671: | ||
(println "You chose: " |
(println "You chose: " |
||
(menu "Which is from the three pigs: " |
(menu "Which is from the three pigs: " |
||
["fee fie" "huff and puff" "mirror mirror" "tick tock"]))</ |
["fee fie" "huff and puff" "mirror mirror" "tick tock"]))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. Test-Prompt-Menu. |
PROGRAM-ID. Test-Prompt-Menu. |
||
Line 765: | Line 765: | ||
. |
. |
||
END PROGRAM Prompt-Menu.</ |
END PROGRAM Prompt-Menu.</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun select (prompt choices) |
||
(if (null choices) |
(if (null choices) |
||
"" |
"" |
||
Line 780: | Line 780: | ||
(force-output) |
(force-output) |
||
(setf n (parse-integer (read-line *standard-input* nil) |
(setf n (parse-integer (read-line *standard-input* nil) |
||
:junk-allowed t)))))</ |
:junk-allowed t)))))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.conv, std.string, std.array, std.typecons; |
||
string menuSelect(in string[] entries) { |
string menuSelect(in string[] entries) { |
||
Line 817: | Line 817: | ||
"mirror mirror", "tick tock"]; |
"mirror mirror", "tick tock"]; |
||
writeln("You chose '", items.menuSelect, "'."); |
writeln("You chose '", items.menuSelect, "'."); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Choose one: |
<pre>Choose one: |
||
Line 829: | Line 829: | ||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
{{Trans|Go}} |
{{Trans|Go}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Menu; |
program Menu; |
||
Line 859: | Line 859: | ||
'tick tock'], 'Enter number: ')); |
'tick tock'], 'Enter number: ')); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 878: | Line 878: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Menu do |
||
def select(_, []), do: "" |
def select(_, []), do: "" |
||
def select(prompt, items) do |
def select(prompt, items) do |
||
Line 898: | Line 898: | ||
items = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
items = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
||
response = Menu.select("Which is from the three pigs", items) |
response = Menu.select("Which is from the three pigs", items) |
||
IO.puts "you chose: #{inspect response}"</ |
IO.puts "you chose: #{inspect response}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 919: | Line 919: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROCEDURE Selection(choices$[],prompt$->sel$) |
PROCEDURE Selection(choices$[],prompt$->sel$) |
||
IF UBOUND(choices$,1)-LBOUND(choices$,1)=0 THEN |
IF UBOUND(choices$,1)-LBOUND(choices$,1)=0 THEN |
||
Line 936: | Line 936: | ||
sel$=ret$ |
sel$=ret$ |
||
END PROCEDURE |
END PROCEDURE |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">include get.e |
||
function menu_select(sequence items, object prompt) |
function menu_select(sequence items, object prompt) |
||
Line 960: | Line 960: | ||
constant prompt = "Which is from the three pigs? " |
constant prompt = "Which is from the three pigs? " |
||
printf(1,"You chose %s.\n",{menu_select(items,prompt)})</ |
printf(1,"You chose %s.\n",{menu_select(items,prompt)})</syntaxhighlight> |
||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
let rec menuChoice (options : string list) prompt = |
let rec menuChoice (options : string list) prompt = |
||
Line 984: | Line 984: | ||
printfn "You chose: %s" choice |
printfn "You chose: %s" choice |
||
0</ |
0</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: formatting io kernel math math.parser sequences ; |
||
: print-menu ( seq -- ) |
: print-menu ( seq -- ) |
||
Line 999: | Line 999: | ||
] [ drop (select) ] if* ; |
] [ drop (select) ] if* ; |
||
: select ( seq -- result ) [ "" ] [ (select) ] if-empty ;</ |
: select ( seq -- result ) [ "" ] [ (select) ] if-empty ;</syntaxhighlight> |
||
Example usage: |
Example usage: |
||
Line 1,014: | Line 1,014: | ||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
{{incorrect|Fantom|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
{{incorrect|Fantom|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
||
< |
<syntaxhighlight lang="fantom">class Main |
||
{ |
{ |
||
static Void displayList (Str[] items) |
static Void displayList (Str[] items) |
||
Line 1,049: | Line 1,049: | ||
echo ("You chose: $choice") |
echo ("You chose: $choice") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
===Idiomatic Forth=== |
===Idiomatic Forth=== |
||
Out of the box Forth does not have lists. This version uses strings and a vector table, which arguably is more how one would do this task in Forth. It returns a nil string if a nil string is given otherwise the input string becomes the title of the menu. |
Out of the box Forth does not have lists. This version uses strings and a vector table, which arguably is more how one would do this task in Forth. It returns a nil string if a nil string is given otherwise the input string becomes the title of the menu. |
||
< |
<syntaxhighlight lang="forth">\ Rosetta Code Menu Idiomatic Forth |
||
\ vector table compiler |
\ vector table compiler |
||
Line 1,095: | Line 1,095: | ||
CR [CHAR] 0 - SELECT |
CR [CHAR] 0 - SELECT |
||
THEN |
THEN |
||
;</ |
;</syntaxhighlight> |
||
===If there must be lists=== |
===If there must be lists=== |
||
Here we extend Forth to support simple lists and complete the task using the language extensions. |
Here we extend Forth to support simple lists and complete the task using the language extensions. |
||
< |
<syntaxhighlight lang="forth">\ Rosetta Menu task with Simple lists in Forth |
||
: STRING, ( caddr len -- ) HERE OVER CHAR+ ALLOT PLACE ; |
: STRING, ( caddr len -- ) HERE OVER CHAR+ ALLOT PLACE ; |
||
Line 1,162: | Line 1,162: | ||
CR SWAP {NTH} |
CR SWAP {NTH} |
||
THEN |
THEN |
||
;</ |
;</syntaxhighlight> |
||
Test at the gForth console |
Test at the gForth console |
||
Line 1,193: | Line 1,193: | ||
Please find the build instructions in the comments at the start of the FORTRAN 2008 source. Compiler: gfortran from the GNU compiler collection. Command interpreter: bash. |
Please find the build instructions in the comments at the start of the FORTRAN 2008 source. Compiler: gfortran from the GNU compiler collection. Command interpreter: bash. |
||
<syntaxhighlight lang="fortran"> |
|||
<lang FORTRAN> |
|||
!a=./f && make $a && OMP_NUM_THREADS=2 $a |
!a=./f && make $a && OMP_NUM_THREADS=2 $a |
||
!gfortran -std=f2008 -Wall -fopenmp -ffree-form -fall-intrinsics -fimplicit-none f.f08 -o f |
!gfortran -std=f2008 -Wall -fopenmp -ffree-form -fall-intrinsics -fimplicit-none f.f08 -o f |
||
Line 1,251: | Line 1,251: | ||
end program menu_demo |
end program menu_demo |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">dim as string menu(1 to 4)={ "fee fie", "huff and puff", "mirror mirror", "tick tock" } |
||
function menu_select( m() as string ) as string |
function menu_select( m() as string ) as string |
||
Line 1,272: | Line 1,272: | ||
end function |
end function |
||
print menu_select( menu() )</ |
print menu_select( menu() )</syntaxhighlight> |
||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
< |
<syntaxhighlight lang="gambas"> |
||
Public Sub Main() |
Public Sub Main() |
||
Line 1,326: | Line 1,326: | ||
End |
End |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,365: | Line 1,365: | ||
pick = menu(choices, "Enter number: ") |
pick = menu(choices, "Enter number: ") |
||
fmt.Printf("You picked %q\n", pick) |
fmt.Printf("You picked %q\n", pick) |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,379: | Line 1,379: | ||
=={{header|GW-BASIC}}== |
=={{header|GW-BASIC}}== |
||
< |
<syntaxhighlight lang="gwbasic"> |
||
10 DATA "Fee fie", "Huff and Puff", "Mirror mirror", "Tick tock" |
10 DATA "Fee fie", "Huff and Puff", "Mirror mirror", "Tick tock" |
||
20 VC = 0 |
20 VC = 0 |
||
Line 1,396: | Line 1,396: | ||
150 PRINT "You picked ", M$(VC-1) |
150 PRINT "You picked ", M$(VC-1) |
||
160 END |
160 END |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">module RosettaSelect where |
||
import Data.Maybe (listToMaybe) |
import Data.Maybe (listToMaybe) |
||
Line 1,422: | Line 1,422: | ||
maybeRead :: Read a => String -> Maybe a |
maybeRead :: Read a => String -> Maybe a |
||
maybeRead = fmap fst . listToMaybe . filter (null . snd) . reads</ |
maybeRead = fmap fst . listToMaybe . filter (null . snd) . reads</syntaxhighlight> |
||
Example usage, at the GHCI prompt: |
Example usage, at the GHCI prompt: |
||
< |
<syntaxhighlight lang="haskell">*RosettaSelect> select ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
||
1) fee fie |
1) fee fie |
||
2) huff and puff |
2) huff and puff |
||
Line 1,432: | Line 1,432: | ||
Choose an item: 3 |
Choose an item: 3 |
||
"mirror mirror" |
"mirror mirror" |
||
*RosettaSelect></ |
*RosettaSelect></syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
{{incorrect|HicEst|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
{{incorrect|HicEst|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
||
< |
<syntaxhighlight lang="hicest">CHARACTER list = "fee fie,huff and puff,mirror mirror,tick tock,", answer*20 |
||
POP(Menu=list, SelTxt=answer) |
POP(Menu=list, SelTxt=answer) |
||
Line 1,444: | Line 1,444: | ||
! The global variable $$ returns the selected list index |
! The global variable $$ returns the selected list index |
||
WRITE(Messagebox, Name) answer, $$ |
WRITE(Messagebox, Name) answer, $$ |
||
END</ |
END</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Line 1,453: | Line 1,453: | ||
procedure main() shows how to call the choose_from_menu "function", which demonstrates use of differing menu lists and a empty list. |
procedure main() shows how to call the choose_from_menu "function", which demonstrates use of differing menu lists and a empty list. |
||
<syntaxhighlight lang="icon"> |
|||
<lang Icon> |
|||
## menu.icn : rewrite of the faulty version on Rosetta Code site 24/4/2021 |
## menu.icn : rewrite of the faulty version on Rosetta Code site 24/4/2021 |
||
Line 1,500: | Line 1,500: | ||
end ## displaymenu(X) |
end ## displaymenu(X) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
CHOICES =: ];._2 'fee fie;huff and puff;mirror mirror;tick tock;' |
CHOICES =: ];._2 'fee fie;huff and puff;mirror mirror;tick tock;' |
||
PROMPT =: 'Which is from the three pigs? ' |
PROMPT =: 'Which is from the three pigs? ' |
||
Line 1,524: | Line 1,524: | ||
RESULT {:: CHOICES |
RESULT {:: CHOICES |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
See [[Talk:Select#J_implementation|Talk page]] for explanation. |
See [[Talk:Select#J_implementation|Talk page]] for explanation. |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java5">public static String select(List<String> list, String prompt){ |
||
if(list.size() == 0) return ""; |
if(list.size() == 0) return ""; |
||
Scanner sc = new Scanner(System.in); |
Scanner sc = new Scanner(System.in); |
||
Line 1,544: | Line 1,544: | ||
}while(ret == null); |
}while(ret == null); |
||
return ret; |
return ret; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
{{works with|Node.js}} |
{{works with|Node.js}} |
||
< |
<syntaxhighlight lang="javascript">const readline = require('readline'); |
||
async function menuSelect(question, choices) { |
async function menuSelect(question, choices) { |
||
Line 1,583: | Line 1,583: | ||
menuSelect(question, choices).then((answer) => { |
menuSelect(question, choices).then((answer) => { |
||
console.log(`\nYou chose ${answer}`); |
console.log(`\nYou chose ${answer}`); |
||
});</ |
});</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|1.5}} |
{{works with|jq|1.5}} |
||
This version uses jq 1.5's 'input' builtin to read programmatically from STDIN. |
This version uses jq 1.5's 'input' builtin to read programmatically from STDIN. |
||
< |
<syntaxhighlight lang="jq">def choice: |
||
def read(prompt; max): |
def read(prompt; max): |
||
def __read__: |
def __read__: |
||
Line 1,606: | Line 1,606: | ||
| if ($read|type) == "string" then $read |
| if ($read|type) == "string" then $read |
||
else "Thank you for selecting \($in[$read-1])" end |
else "Thank you for selecting \($in[$read-1])" end |
||
end ;</ |
end ;</syntaxhighlight> |
||
'''Example:''' |
'''Example:''' |
||
< |
<syntaxhighlight lang="jq">["fee fie", "huff and puff", "mirror mirror", "tick tock"] | choice</syntaxhighlight> |
||
<syntaxhighlight lang="sh"> |
|||
<lang sh> |
|||
$ jq -n -r -f Menu.jq |
$ jq -n -r -f Menu.jq |
||
Enter your choice: |
Enter your choice: |
||
Line 1,625: | Line 1,625: | ||
1 |
1 |
||
Thank you for selecting fee fie</ |
Thank you for selecting fee fie</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="julia">using Printf |
||
function _menu(items) |
function _menu(items) |
||
Line 1,659: | Line 1,659: | ||
items = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
items = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
||
item = _selector(items, "Which is from the three pigs: ") |
item = _selector(items, "Which is from the three pigs: ") |
||
println("You chose: ", item)</ |
println("You chose: ", item)</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun menu(list: List<String>): String { |
fun menu(list: List<String>): String { |
||
Line 1,686: | Line 1,686: | ||
val choice = menu(list) |
val choice = menu(list) |
||
println("\nYou chose : $choice") |
println("\nYou chose : $choice") |
||
}</ |
}</syntaxhighlight> |
||
Sample session: |
Sample session: |
||
{{out}} |
{{out}} |
||
Line 1,722: | Line 1,722: | ||
=={{header|langur}}== |
=={{header|langur}}== |
||
{{works with|langur|0.7.1}} |
{{works with|langur|0.7.1}} |
||
< |
<syntaxhighlight lang="langur">val .select = f(.entries) { |
||
if not isArray(.entries): throw "invalid args" |
if not isArray(.entries): throw "invalid args" |
||
if len(.entries) == 0: return ZLS |
if len(.entries) == 0: return ZLS |
||
Line 1,742: | Line 1,742: | ||
} |
} |
||
writeln .select(["fee fie", "eat pi", "huff and puff", "tick tock"])</ |
writeln .select(["fee fie", "eat pi", "huff and puff", "tick tock"])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,757: | Line 1,757: | ||
{{incorrect|Logo|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
{{incorrect|Logo|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
||
{{works with|UCB Logo}} |
{{works with|UCB Logo}} |
||
< |
<syntaxhighlight lang="logo">to select :prompt [:options] |
||
foreach :options [(print # ?)] |
foreach :options [(print # ?)] |
||
forever [ |
forever [ |
||
Line 1,770: | Line 1,770: | ||
[Which is from the three pigs?] |
[Which is from the three pigs?] |
||
[fee fie] [huff and puff] [mirror mirror] [tick tock]) |
[fee fie] [huff and puff] [mirror mirror] [tick tock]) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function select (list) |
||
if not list or #list == 0 then |
if not list or #list == 0 then |
||
return "" |
return "" |
||
Line 1,790: | Line 1,790: | ||
print("Nothing:", select {}) |
print("Nothing:", select {}) |
||
print() |
print() |
||
print("You chose:", select {"fee fie", "huff and puff", "mirror mirror", "tick tock"})</ |
print("You chose:", select {"fee fie", "huff and puff", "mirror mirror", "tick tock"})</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,829: | Line 1,829: | ||
Redisplays the list of choices on every invalid input as per the task description. In the notebook interface (of Wolfram Desktop, at least), Print[] would most pragmatically be located outside of the loop because Input[] uses a dialog box. |
Redisplays the list of choices on every invalid input as per the task description. In the notebook interface (of Wolfram Desktop, at least), Print[] would most pragmatically be located outside of the loop because Input[] uses a dialog box. |
||
< |
<syntaxhighlight lang="mathematica">textMenu[data_List] := Module[{choice}, |
||
If[Length@data == 0, Return@""]; |
If[Length@data == 0, Return@""]; |
||
While[!(IntegerQ@choice && Length@data >= choice > 0), |
While[!(IntegerQ@choice && Length@data >= choice > 0), |
||
Line 1,836: | Line 1,836: | ||
]; |
]; |
||
data[[choice]] |
data[[choice]] |
||
]</ |
]</syntaxhighlight> |
||
{{out|Kernel (REPL) output|note=function definition omitted}} |
{{out|Kernel (REPL) output|note=function definition omitted}} |
||
<pre>Wolfram Desktop Kernel (using Wolfram Language 12.0.0) for Microsoft Windows (64-bit) |
<pre>Wolfram Desktop Kernel (using Wolfram Language 12.0.0) for Microsoft Windows (64-bit) |
||
Line 1,877: | Line 1,877: | ||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">function sucess = menu(list) |
||
if numel(list) == 0 |
if numel(list) == 0 |
||
Line 1,915: | Line 1,915: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
min has an operator <code>choose</code> that nearly conforms to this task. The input list is altered so that the choice can be returned, and the empty list case is handled. |
min has an operator <code>choose</code> that nearly conforms to this task. The input list is altered so that the choice can be returned, and the empty list case is handled. |
||
< |
<syntaxhighlight lang="min">( |
||
:prompt =list |
:prompt =list |
||
(list bool) |
(list bool) |
||
Line 1,929: | Line 1,929: | ||
("fee fie" "huff and puff" "mirror mirror" "tick tock") |
("fee fie" "huff and puff" "mirror mirror" "tick tock") |
||
"Enter an option" menu |
"Enter an option" menu |
||
"You chose: " print! puts!</ |
"You chose: " print! puts!</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,949: | Line 1,949: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
{{incorrect|Modula-2|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
{{incorrect|Modula-2|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
||
< |
<syntaxhighlight lang="modula2">MODULE Menu; |
||
FROM InOut IMPORT WriteString, WriteCard, WriteLn, ReadCard; |
FROM InOut IMPORT WriteString, WriteCard, WriteLn, ReadCard; |
||
Line 1,986: | Line 1,986: | ||
WriteLn; |
WriteLn; |
||
END (*of IF*) |
END (*of IF*) |
||
END Menu.</ |
END Menu.</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
< |
<syntaxhighlight lang="mumps">MENU(STRINGS,SEP) |
||
;http://rosettacode.org/wiki/Menu |
;http://rosettacode.org/wiki/Menu |
||
NEW I,A,MAX |
NEW I,A,MAX |
||
Line 2,003: | Line 2,003: | ||
IF (A<1)!(A>MAX)!(A\1'=A) GOTO WRITEMENU |
IF (A<1)!(A>MAX)!(A\1'=A) GOTO WRITEMENU |
||
KILL I,MAX |
KILL I,MAX |
||
QUIT $PIECE(STRINGS,SEP,A)</ |
QUIT $PIECE(STRINGS,SEP,A)</syntaxhighlight> |
||
Usage:<pre> |
Usage:<pre> |
||
USER>W !,$$MENU^ROSETTA("fee fie^huff and puff^mirror mirror^tick tock","^") |
USER>W !,$$MENU^ROSETTA("fee fie^huff and puff^mirror mirror^tick tock","^") |
||
Line 2,045: | Line 2,045: | ||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
{{trans|Ursa}} |
{{trans|Ursa}} |
||
< |
<syntaxhighlight lang="nanoquery">def _menu($items) |
||
for ($i = 0) ($i < len($items)) ($i = $i + 1) |
for ($i = 0) ($i < len($items)) ($i = $i + 1) |
||
println " " + $i + ") " + $items[$i] |
println " " + $i + ") " + $items[$i] |
||
Line 2,078: | Line 2,078: | ||
append $items "fee fie" "huff and puff" "mirror mirror" "tick tock" |
append $items "fee fie" "huff and puff" "mirror mirror" "tick tock" |
||
$item = selector($items, "Which is from the three pigs: ") |
$item = selector($items, "Which is from the three pigs: ") |
||
println "You chose: " + $item</ |
println "You chose: " + $item</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">import strutils, rdstdin |
||
proc menu(xs: openArray[string]) = |
proc menu(xs: openArray[string]) = |
||
Line 2,103: | Line 2,103: | ||
const xs = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
const xs = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
||
let item = selector(xs, "Which is from the three pigs: ") |
let item = selector(xs, "Which is from the three pigs: ") |
||
echo "You chose: ", item</ |
echo "You chose: ", item</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,124: | Line 2,124: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml"> |
||
let select ?(prompt="Choice? ") = function |
let select ?(prompt="Choice? ") = function |
||
| [] -> "" |
| [] -> "" |
||
Line 2,134: | Line 2,134: | ||
with _ -> menu () |
with _ -> menu () |
||
in menu () |
in menu () |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example use in the REPL: |
Example use in the REPL: |
||
< |
<syntaxhighlight lang="ocaml"> |
||
# select ["fee fie"; "huff and puff"; "mirror mirror"; "tick tock"];; |
# select ["fee fie"; "huff and puff"; "mirror mirror"; "tick tock"];; |
||
0: fee fie |
0: fee fie |
||
Line 2,145: | Line 2,145: | ||
Choice? 2 |
Choice? 2 |
||
- : string = "mirror mirror" |
- : string = "mirror mirror" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OpenEdge/Progress}}== |
=={{header|OpenEdge/Progress}}== |
||
< |
<syntaxhighlight lang="progress">FUNCTION bashMenu RETURNS CHAR( |
||
i_c AS CHAR |
i_c AS CHAR |
||
): |
): |
||
Line 2,197: | Line 2,197: | ||
MESSAGE |
MESSAGE |
||
bashMenu( "fee fie,huff and puff,mirror mirror,tick tock" ) |
bashMenu( "fee fie,huff and puff,mirror mirror,tick tock" ) |
||
VIEW-AS ALERT-BOX.</ |
VIEW-AS ALERT-BOX.</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">declare |
||
fun {Select Prompt Items} |
fun {Select Prompt Items} |
||
case Items of nil then "" |
case Items of nil then "" |
||
Line 2,230: | Line 2,230: | ||
in |
in |
||
{System.showInfo "You chose: "#Item}</ |
{System.showInfo "You chose: "#Item}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
{{incorrect|PARI/GP|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
{{incorrect|PARI/GP|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
||
< |
<syntaxhighlight lang="parigp">choose(v)=my(n);for(i=1,#v,print(i". "v[i]));while(type(n=input())!="t_INT"|n>#v|n<1,);v[n] |
||
choose(["fee fie","huff and puff","mirror mirror","tick tock"])</ |
choose(["fee fie","huff and puff","mirror mirror","tick tock"])</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free_Pascal}} |
{{works with|Free_Pascal}} |
||
Tested with Free Pascal 2.6.4 (arm). |
Tested with Free Pascal 2.6.4 (arm). |
||
< |
<syntaxhighlight lang="pascal">program Menu; |
||
{$ASSERTIONS ON} |
{$ASSERTIONS ON} |
||
uses |
uses |
||
Line 2,300: | Line 2,300: | ||
dispose(MenuItems, Done); |
dispose(MenuItems, Done); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,324: | Line 2,324: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub menu |
||
{ |
{ |
||
my ($prompt,@array) = @_; |
my ($prompt,@array) = @_; |
||
Line 2,341: | Line 2,341: | ||
$a = &menu($prompt,@a); |
$a = &menu($prompt,@a); |
||
print "You chose: $a\n";</ |
print "You chose: $a\n";</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
< |
<syntaxhighlight lang="phix">function menu_select(sequence items, object prompt) |
||
sequence res = "" |
sequence res = "" |
||
items = remove_all("",items) |
items = remove_all("",items) |
||
Line 2,370: | Line 2,370: | ||
constant prompt = "Which is from the three pigs? " |
constant prompt = "Which is from the three pigs? " |
||
string res = menu_select(items,prompt) |
string res = menu_select(items,prompt) |
||
printf(1,"You chose %s.\n",{res})</ |
printf(1,"You chose %s.\n",{res})</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
$stdin = fopen("php://stdin", "r"); |
$stdin = fopen("php://stdin", "r"); |
||
$allowed = array(1 => 'fee fie', 'huff and puff', 'mirror mirror', 'tick tock'); |
$allowed = array(1 => 'fee fie', 'huff and puff', 'mirror mirror', 'tick tock'); |
||
Line 2,387: | Line 2,387: | ||
break; |
break; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de choose (Prompt Items) |
||
(use N |
(use N |
||
(loop |
(loop |
||
Line 2,400: | Line 2,400: | ||
(T (>= (length Items) N 1) (prinl (get Items N))) ) ) ) |
(T (>= (length Items) N 1) (prinl (get Items N))) ) ) ) |
||
(choose "Which is from the three pigs?" |
(choose "Which is from the three pigs?" |
||
'("fee fie" "huff and puff" "mirror mirror" "tick tock") )</ |
'("fee fie" "huff and puff" "mirror mirror" "tick tock") )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,423: | Line 2,423: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
{{incorrect|PL/I|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
{{incorrect|PL/I|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
test: proc options (main); |
test: proc options (main); |
||
Line 2,442: | Line 2,442: | ||
end test; |
end test; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function Select-TextItem |
function Select-TextItem |
||
{ |
{ |
||
Line 2,523: | Line 2,523: | ||
“fee fie”, “huff and puff”, “mirror mirror”, “tick tock” | Select-TextItem -Prompt "Select a string" |
“fee fie”, “huff and puff”, “mirror mirror”, “tick tock” | Select-TextItem -Prompt "Select a string" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 2,538: | Line 2,538: | ||
=={{header|ProDOS}}== |
=={{header|ProDOS}}== |
||
{{incorrect|ProDOS|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
{{incorrect|ProDOS|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
||
<lang> |
<syntaxhighlight lang="text"> |
||
:a |
:a |
||
printline ==========MENU========== |
printline ==========MENU========== |
||
Line 2,552: | Line 2,552: | ||
printline You either chose an invalid choice or didn't chose. |
printline You either chose an invalid choice or didn't chose. |
||
editvar /newvar /value=goback /userinput=1 /title=Do you want to chose something else? |
editvar /newvar /value=goback /userinput=1 /title=Do you want to chose something else? |
||
if -goback- /hasvalue y goto :a else exitcurrentprogram 1 </ |
if -goback- /hasvalue y goto :a else exitcurrentprogram 1 </syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI-Prolog|6}} |
{{works with|SWI-Prolog|6}} |
||
< |
<syntaxhighlight lang="prolog"> |
||
rosetta_menu([], "") :- !. %% Incase of an empty list. |
rosetta_menu([], "") :- !. %% Incase of an empty list. |
||
rosetta_menu(Items, SelectedItem) :- |
rosetta_menu(Items, SelectedItem) :- |
||
Line 2,574: | Line 2,574: | ||
prompt1('Select a menu item by number:'), |
prompt1('Select a menu item by number:'), |
||
read(Choice). |
read(Choice). |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example run: |
Example run: |
||
< |
<syntaxhighlight lang="prolog"> |
||
?- rosetta_menu(["fee fie", "huff and puff", "mirror mirror", "tick tock"], String). |
?- rosetta_menu(["fee fie", "huff and puff", "mirror mirror", "tick tock"], String). |
||
Line 2,599: | Line 2,599: | ||
Select a menu item by number:3. |
Select a menu item by number:3. |
||
String = "mirror mirror". |
String = "mirror mirror". |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">If OpenConsole() |
||
Define i, txt$, choice |
Define i, txt$, choice |
||
Dim txts.s(4) |
Dim txts.s(4) |
||
Line 2,628: | Line 2,628: | ||
TheStrings: |
TheStrings: |
||
Data.s "fee fie", "huff And puff", "mirror mirror", "tick tock" |
Data.s "fee fie", "huff And puff", "mirror mirror", "tick tock" |
||
EndDataSection</ |
EndDataSection</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">def _menu(items): |
||
for indexitem in enumerate(items): |
for indexitem in enumerate(items): |
||
print (" %2i) %s" % indexitem) |
print (" %2i) %s" % indexitem) |
||
Line 2,657: | Line 2,657: | ||
items = ['fee fie', 'huff and puff', 'mirror mirror', 'tick tock'] |
items = ['fee fie', 'huff and puff', 'mirror mirror', 'tick tock'] |
||
item = selector(items, 'Which is from the three pigs: ') |
item = selector(items, 'Which is from the three pigs: ') |
||
print ("You chose: " + item)</ |
print ("You chose: " + item)</syntaxhighlight> |
||
Sample runs: |
Sample runs: |
||
Line 2,688: | Line 2,688: | ||
Uses [http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/base/html/menu.html menu]. |
Uses [http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/base/html/menu.html menu]. |
||
< |
<syntaxhighlight lang="r">showmenu <- function(choices = NULL) |
||
{ |
{ |
||
if (is.null(choices)) return("") |
if (is.null(choices)) return("") |
||
Line 2,697: | Line 2,697: | ||
str <- showmenu(c("fee fie", "huff and puff", "mirror mirror", "tick tock")) |
str <- showmenu(c("fee fie", "huff and puff", "mirror mirror", "tick tock")) |
||
str <- showmenu() |
str <- showmenu() |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
Line 2,715: | Line 2,715: | ||
(menu '("fee fie" "huff and puff" "mirror mirror" "tick tock")) |
(menu '("fee fie" "huff and puff" "mirror mirror" "tick tock")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sample Run: |
Sample Run: |
||
Line 2,744: | Line 2,744: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>sub menu ( $prompt, @items ) { |
||
return '' unless @items.elems; |
return '' unless @items.elems; |
||
repeat until my $selection ~~ /^ \d+ $/ && @items[--$selection] { |
repeat until my $selection ~~ /^ \d+ $/ && @items[--$selection] { |
||
Line 2,761: | Line 2,761: | ||
$answer = menu( $prompt, @choices ); |
$answer = menu( $prompt, @choices ); |
||
say "You chose: $answer" if $answer.chars;</ |
say "You chose: $answer" if $answer.chars;</syntaxhighlight> |
||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
{{incorrect|REBOL|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
{{incorrect|REBOL|The function should return an empty string if called with an empty list. Please also check if this could really used as a [https://en.wikipedia.org/wiki/Subroutine function aka subroutine.]}} |
||
< |
<syntaxhighlight lang="rebol">REBOL [ |
||
Title: "Text Menu" |
Title: "Text Menu" |
||
URL: http://rosettacode.org/wiki/Select |
URL: http://rosettacode.org/wiki/Select |
||
Line 2,785: | Line 2,785: | ||
choice: ask "Which is from the three pigs? " |
choice: ask "Which is from the three pigs? " |
||
] |
] |
||
print ["You chose:" pick choices to-integer choice]</ |
print ["You chose:" pick choices to-integer choice]</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,806: | Line 2,806: | ||
You chose: huff and puff</pre> |
You chose: huff and puff</pre> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="red">Red ["text menu"] |
||
menu: function [items][ |
menu: function [items][ |
||
Line 2,813: | Line 2,813: | ||
attempt [pick items to-integer ask "Your choice: "] |
attempt [pick items to-integer ask "Your choice: "] |
||
]] |
]] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,839: | Line 2,839: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program displays a list, then prompts the user for a selection number (integer).*/ |
||
do forever /*keep prompting until response is OK. */ |
do forever /*keep prompting until response is OK. */ |
||
call list_create /*create the list from scratch. */ |
call list_create /*create the list from scratch. */ |
||
Line 2,875: | Line 2,875: | ||
return |
return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
sayErr: say; say '***error***' arg(1) x; say; return</ |
sayErr: say; say '***error***' arg(1) x; say; return</syntaxhighlight> |
||
{{out|output|text= (which includes what the user entered):}} |
{{out|output|text= (which includes what the user entered):}} |
||
<pre> |
<pre> |
||
Line 2,890: | Line 2,890: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
aList = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
aList = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
||
selected = menu(aList, "please make a selection: ") |
selected = menu(aList, "please make a selection: ") |
||
Line 2,910: | Line 2,910: | ||
end |
end |
||
return aList[index] |
return aList[index] |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,928: | Line 2,928: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby"> |
||
def select(prompt, items = []) |
def select(prompt, items = []) |
||
if items.empty? |
if items.empty? |
||
Line 2,955: | Line 2,955: | ||
response = select('Which is from the three pigs', items) |
response = select('Which is from the three pigs', items) |
||
puts "you chose: >#{response}<" |
puts "you chose: >#{response}<" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">dim choose$(5) |
||
choose$(1) = "1 Fee Fie" |
choose$(1) = "1 Fee Fie" |
||
choose$(2) = "2 Huff Puff" |
choose$(2) = "2 Huff Puff" |
||
Line 2,988: | Line 2,988: | ||
cls |
cls |
||
end |
end |
||
end if</ |
end if</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
fn menu_select<'a>(items: &'a [&'a str]) -> &'a str { |
fn menu_select<'a>(items: &'a [&'a str]) -> &'a str { |
||
if items.len() == 0 { |
if items.len() == 0 { |
||
Line 3,039: | Line 3,039: | ||
println!("You chose: {}", selection); |
println!("You chose: {}", selection); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===Scala idiom (Functional)=== |
===Scala idiom (Functional)=== |
||
<lang>import scala.util.Try |
<syntaxhighlight lang="text">import scala.util.Try |
||
object Menu extends App { |
object Menu extends App { |
||
Line 3,070: | Line 3,070: | ||
println(s"\nYou chose : $choice") |
println(s"\nYou chose : $choice") |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func string: menuSelect (in array string: items, in string: prompt) is func |
const func string: menuSelect (in array string: items, in string: prompt) is func |
||
Line 3,101: | Line 3,101: | ||
begin |
begin |
||
writeln("You chose " <& menuSelect(items, prompt)); |
writeln("You chose " <& menuSelect(items, prompt)); |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func menu (prompt, arr) { |
||
arr.len > 0 || return '' |
arr.len > 0 || return '' |
||
loop { |
loop { |
||
Line 3,120: | Line 3,120: | ||
var answer = menu(prompt, list) |
var answer = menu(prompt, list) |
||
say "You choose: #{answer}"</ |
say "You choose: #{answer}"</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func getMenuInput(selections: [String]) -> String { |
||
guard !selections.isEmpty else { |
guard !selections.isEmpty else { |
||
return "" |
return "" |
||
Line 3,159: | Line 3,159: | ||
]) |
]) |
||
print("You chose: \(selected)")</ |
print("You chose: \(selected)")</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc select {prompt choices} { |
||
set nc [llength $choices] |
set nc [llength $choices] |
||
if {!$nc} { |
if {!$nc} { |
||
Line 3,181: | Line 3,181: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Testing it out interactively... |
Testing it out interactively... |
||
< |
<syntaxhighlight lang="tcl">% puts >[select test {}]< |
||
>< |
>< |
||
% puts >[select "Which is from the three pigs" { |
% puts >[select "Which is from the three pigs" { |
||
Line 3,213: | Line 3,213: | ||
4: tick tock |
4: tick tock |
||
Which is from the three pigs: 2 |
Which is from the three pigs: 2 |
||
>huff and puff<</ |
>huff and puff<</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
Line 3,244: | Line 3,244: | ||
Output with <tt>FEE FIE:HUFF AND PUFF:MIRROR MIRROR:TICK TOCK</tt> |
Output with <tt>FEE FIE:HUFF AND PUFF:MIRROR MIRROR:TICK TOCK</tt> |
||
<lang> 1:FEE FIE |
<syntaxhighlight lang="text"> 1:FEE FIE |
||
2:HUFF AND PUFF |
2:HUFF AND PUFF |
||
3:MIRROR MIRROR |
3:MIRROR MIRROR |
||
4:TICK TOCK |
4:TICK TOCK |
||
? [flashing cursor]</ |
? [flashing cursor]</syntaxhighlight> |
||
Line 3,270: | Line 3,270: | ||
=={{header|True BASIC}}== |
=={{header|True BASIC}}== |
||
< |
<syntaxhighlight lang="qbasic">DIM menu$(4) |
||
MAT READ menu$ |
MAT READ menu$ |
||
DATA "fee fie", "huff and puff", "mirror mirror", "tick tock" |
DATA "fee fie", "huff and puff", "mirror mirror", "tick tock" |
||
Line 3,289: | Line 3,289: | ||
PRINT sel$(menu$()) |
PRINT sel$(menu$()) |
||
END</ |
END</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 3,303: | Line 3,303: | ||
{{works with|pdksh}} |
{{works with|pdksh}} |
||
{{works with|zsh}} |
{{works with|zsh}} |
||
< |
<syntaxhighlight lang="bash"># choose 'choice 1' 'choice 2' ... |
||
# Prints menu to standard error. Prompts with PS3. |
# Prints menu to standard error. Prompts with PS3. |
||
# Reads REPLY from standard input. Sets CHOICE. |
# Reads REPLY from standard input. Sets CHOICE. |
||
Line 3,321: | Line 3,321: | ||
choose 'fee fie' 'huff and puff' 'mirror mirror' 'tick tock' |
choose 'fee fie' 'huff and puff' 'mirror mirror' 'tick tock' |
||
[[ -n $CHOICE ]] && echo You chose: $CHOICE |
[[ -n $CHOICE ]] && echo You chose: $CHOICE |
||
[[ -z $CHOICE ]] && echo No input.</ |
[[ -z $CHOICE ]] && echo No input.</syntaxhighlight> |
||
<pre>$ bash menu.sh |
<pre>$ bash menu.sh |
||
Line 3,345: | Line 3,345: | ||
* ''Deviation from task:'' When the list of choices is empty, this function returns an empty list, not an empty string. |
* ''Deviation from task:'' When the list of choices is empty, this function returns an empty list, not an empty string. |
||
< |
<syntaxhighlight lang="es"># choose 'choice 1' 'choice 2' ... |
||
# Prints menu to standard error. Prompts with $prompt. |
# Prints menu to standard error. Prompts with $prompt. |
||
# Returns choice. If no input, returns empty list. |
# Returns choice. If no input, returns empty list. |
||
Line 3,398: | Line 3,398: | ||
~ $#choice 1 && echo You chose: $choice |
~ $#choice 1 && echo You chose: $choice |
||
~ $#choice 0 && echo No input. |
~ $#choice 0 && echo No input. |
||
}</ |
}</syntaxhighlight> |
||
<pre>$ es menu.es |
<pre>$ es menu.es |
||
Line 3,410: | Line 3,410: | ||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ursa">def _menu (string<> items) |
||
for (decl int i) (< i (size items)) (inc i) |
for (decl int i) (< i (size items)) (inc i) |
||
out " " i ") " items<i> endl console |
out " " i ") " items<i> endl console |
||
Line 3,446: | Line 3,446: | ||
decl string item |
decl string item |
||
set item (selector items "Which is from the three pigs: ") |
set item (selector items "Which is from the three pigs: ") |
||
out "You chose: " item endl console</ |
out "You chose: " item endl console</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vb">'The Function |
||
Function Menu(ArrList, Prompt) |
Function Menu(ArrList, Prompt) |
||
Select Case False 'Non-standard usage hahaha |
Select Case False 'Non-standard usage hahaha |
||
Line 3,484: | Line 3,484: | ||
Sample = Array("fee fie", "huff and puff", "mirror mirror", "tick tock") |
Sample = Array("fee fie", "huff and puff", "mirror mirror", "tick tock") |
||
InputText = "Which is from the three pigs: " |
InputText = "Which is from the three pigs: " |
||
WScript.StdOut.WriteLine("Output: " & Menu(Sample, InputText))</ |
WScript.StdOut.WriteLine("Output: " & Menu(Sample, InputText))</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>C:\>cscript /nologo menu.vbs |
<pre>C:\>cscript /nologo menu.vbs |
||
Line 3,520: | Line 3,520: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-ioutil}} |
{{libheader|Wren-ioutil}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/ioutil" for Input |
||
var menu = Fn.new { |list| |
var menu = Fn.new { |list| |
||
Line 3,534: | Line 3,534: | ||
var list = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
var list = ["fee fie", "huff and puff", "mirror mirror", "tick tock"] |
||
var choice = menu.call(list) |
var choice = menu.call(list) |
||
System.print("\nYou chose : %(choice)")</ |
System.print("\nYou chose : %(choice)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,572: | Line 3,572: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">string 0; |
||
func Menu(List); |
func Menu(List); |
||
Line 3,593: | Line 3,593: | ||
Text(0, Menu([5, "fee fie", "huff and puff", "mirror mirror", "tick tock", |
Text(0, Menu([5, "fee fie", "huff and puff", "mirror mirror", "tick tock", |
||
"Which phrase is from the Three Little Pigs? "]))</ |
"Which phrase is from the Three Little Pigs? "]))</syntaxhighlight> |
||
Example output: |
Example output: |
||
Line 3,608: | Line 3,608: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">// Rosetta Code problem: https://www.rosettacode.org/wiki/Menu |
||
// by Jjuanhdez, 06/2022 |
// by Jjuanhdez, 06/2022 |
||
Line 3,646: | Line 3,646: | ||
label menudata |
label menudata |
||
data "Ack, not good", "fee fie ", "huff and puff" |
data "Ack, not good", "fee fie ", "huff and puff" |
||
data "mirror mirror", "tick tock ", "exit "</ |
data "mirror mirror", "tick tock ", "exit "</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn teleprompter(options){ |
||
os:=T("exit").extend(vm.arglist); N:=os.len(); |
os:=T("exit").extend(vm.arglist); N:=os.len(); |
||
if(N==1) return(""); |
if(N==1) return(""); |
||
Line 3,662: | Line 3,662: | ||
teleprompter("fee fie" , "huff and puff" , "mirror mirror" , "tick tock") |
teleprompter("fee fie" , "huff and puff" , "mirror mirror" , "tick tock") |
||
.println();</ |
.println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |