Menu: Difference between revisions
→{{header|langur}}
Langurmonkey (talk | contribs) |
Langurmonkey (talk | contribs) |
||
(3 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;
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, "'.");
}
{{out}}
<pre>
Choose one:
0) fee fie
1) huff and puff
Line 825 ⟶ 834:
3) tick tock
> 2
You chose 'mirror mirror'.
</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
Line 1,817 ⟶ 1,828:
=={{header|langur}}==
<syntaxhighlight lang="langur">val .select = impure
if .entries is not list: throw "invalid args"
if len(.entries) == 0: return ""
# print the menu
writeln join "\n", map(
val .idx = number read(
"Select entry #: ",
if not
val .y = number .x
.y > 0 and .y <= len(.entries)
|