Menu: Difference between revisions

19 bytes removed ,  16 days ago
 
(11 intermediate revisions by 3 users not shown)
Line 783:
 
=={{header|D}}==
<syntaxhighlight lang="d">import std.stdio, std.conv, std.string, std.array, std.typecons;
import std.stdio, std.conv, std.string, std.array, std.typecons;
 
string menuSelect(in string[] entries) {
Line 791 ⟶ 792:
try {
immutable n = input.to!int;
 
return typeof(return)((n >= 0 && n <= nEntries) ? n : -1);
} catch (Exception e) // Very generic
Line 804 ⟶ 806:
writefln(" %d) %s", i, entry);
"> ".write;
 
immutable input = readln.chomp;
 
immutable choice = validChoice(input, cast(int) (entries.length - 1));
 
if (choice.isNull)
"Wrong choice.".writeln;
Line 816 ⟶ 821:
immutable items = ["fee fie", "huff and puff",
"mirror mirror", "tick tock"];
 
writeln("You chose '", items.menuSelect, "'.");
}
}</syntaxhighlight>
 
{{out}}
<pre>Choose one:
Choose one:
0) fee fie
1) huff and puff
Line 825 ⟶ 834:
3) tick tock
> 2
You chose 'mirror mirror'.</pre>
</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
Line 1,817 ⟶ 1,828:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">
{{works with|langur|0.7.1}}
<syntaxhighlight lang="langur">val .selectchoose = fimpure fn(.entries) {
if entries is not isArray(.entries)list: throw "invalid args"
if len(.not entries) == 0: return ""
 
# print the menu
writeln join ("\n", map(ffn e, i: $"\.{{i:2;}}: \.{{e;}}", .entries, 1..len .(entries)))
 
val .idx = toNumber read(
"Select entry #: ",
ffn(.x) {
if not matching(x -> RE/^[0-9]+$/, .x): return false
val .y = toNumber .x -> number
.y > 0 and .y <= len(.entries)
},
"invalid selection\n", -1,
) -> number
 
.entries[.idx]
}
 
writeln .selectchoose(["fee fie", "eat pi", "huff and puff", "tick tock"])</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 3,685 ⟶ 3,697:
=={{header|Wren}}==
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "./ioutil" for Input
 
var menu = Fn.new { |list|
1,007

edits