Menu: Difference between revisions
Content added Content deleted
m (Added Sidef language) |
(jq) |
||
Line 944: | Line 944: | ||
var choice = select("Which is from the three pigs?", choices); |
var choice = select("Which is from the three pigs?", choices); |
||
WScript.Echo("you selected: " + choice + " -> " + choices[choice]);</lang> |
WScript.Echo("you selected: " + choice + " -> " + choices[choice]);</lang> |
||
=={{header|jq}}== |
|||
{{works with|jq|1.5}} |
|||
This version uses jq 1.5's 'input' builtin to read programmatically from STDIN. |
|||
<lang jq>def choice: |
|||
def read(prompt; max): |
|||
def __read__: |
|||
prompt, |
|||
( input as $input |
|||
| if ($input|type) == "number" and 0 < $input and $input <= max then $input |
|||
else __read__ |
|||
end); |
|||
__read__; |
|||
if length == 0 then "" |
|||
else |
|||
. as $in |
|||
| ("Enter your choice:\n" + |
|||
(reduce range(0; length) as $i (""; . + "\($i + 1): \($in[$i])\n")) ) as $prompt |
|||
| read($prompt; length) as $read |
|||
| if ($read|type) == "string" then $read |
|||
else "Thank you for selecting \($in[$read-1])" end |
|||
end ;</lang> |
|||
'''Example:''' |
|||
<lang jq>["fee fie", "huff and puff", "mirror mirror", "tick tock"] | choice</lang> |
|||
<lang sh> |
|||
$ jq -n -r -f Menu.jq |
|||
Enter your choice: |
|||
1: fee fie |
|||
2: huff and puff |
|||
3: mirror mirror |
|||
4: tick tock |
|||
5 |
|||
Enter your choice: |
|||
1: fee fie |
|||
2: huff and puff |
|||
3: mirror mirror |
|||
4: tick tock |
|||
1 |
|||
Thank you for selecting fee fie</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |