Menu: Difference between revisions

Content added Content deleted
(Added 11l)
(Updated to work with Nim 1.4: added missing parameter types.)
Line 1,786: Line 1,786:
{{trans|Python}}
{{trans|Python}}
<lang nim>import strutils, rdstdin
<lang nim>import strutils, rdstdin

proc menu(xs) =
proc menu(xs: openArray[string]) =
for i,x in xs: echo " ",i,") ",x
for i, x in xs: echo " ", i, ") ", x

proc ok(reply, count): bool =
proc ok(reply: string; count: Positive): bool =
try:
try:
let n = parseInt(reply)
let n = parseInt(reply)
return 0 <= n and n < count
return 0 <= n and n < count
except: return false
except: return false

proc selector(xs, prompt): string =
proc selector(xs: openArray[string]; prompt: string): string =
if xs.len == 0: return ""
if xs.len == 0: return ""
var reply = "-1"
var reply = "-1"
Line 1,803: Line 1,803:
reply = readLineFromStdin(prompt).strip()
reply = readLineFromStdin(prompt).strip()
return xs[parseInt(reply)]
return xs[parseInt(reply)]

const xs = ["fee fie", "huff and puff", "mirror mirror", "tick tock"]
const xs = ["fee fie", "huff and puff", "mirror mirror", "tick tock"]
let item = selector(xs, "Which is from the three pigs: ")
let item = selector(xs, "Which is from the three pigs: ")
echo "You chose: ", item</lang>
echo "You chose: ", item</lang>

Output:
{{out}}
<pre> 0) fee fie
<pre> 0) fee fie
1) huff and puff
1) huff and puff