Menu: Difference between revisions

22 bytes added ,  27 days ago
(5 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 = impure ffn(.entries) {
if .entries is not list: throw "invalid args"
if len(.entries) == 0: return ""
 
# print the menu
writeln join "\n", map(ffn(.e, .i) $"\.i:2;: \.e;", .entries, 1..len .entries)
 
val .idx = toNumbernumber read(
"Select entry #: ",
ffn(.x) {
if not matching(.x -> RE/^[0-9]+$/, .x): return false
val .y = toNumbernumber .x
.y > 0 and .y <= len(.entries)
},
885

edits