Menu: Difference between revisions

15 bytes added ,  16 days ago
 
(9 intermediate revisions by 2 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">val .select = f(.entries) {
val choose = impure if not isListfn(.entries): throw "invalid args"{
if len(.entries) ==is 0not list: returnthrow "invalid args"
if not entries: 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}}
1,007

edits