Given weapons: ["rock" "scissors" "paper"]
If I did player-choice: ask "(r)ock, (p)aper, (s)cissors or (q)uit? "
how could i look for the character entered by the user in the block with word weapons
attached to it
Given weapons: ["rock" "scissors" "paper"]
If I did player-choice: ask "(r)ock, (p)aper, (s)cissors or (q)uit? "
how could i look for the character entered by the user in the block with word weapons
attached to it
If you only want one match, and to use only the actual item names in your block, your own solution is fine. But one of the important things about Red is how you can structure your data to make things easier. For example, if you want to select items from a list based only on a known key (e.g. first character), you can make that explicit, rather than implicit.
weapons: ["r" "rock" "s" "scissors" "p" "paper"]
player-choice: ask "(r)ock, (p)aper, (s)cissors or (q)uit? "
print select weapons player-choice
weapons: ["rock" "scissors" "paper"]
matching-weapon: func [abbrev][
foreach weapon weapons [
if (first weapon) = first abbrev [
return weapon
]
]
]
>> abr: "p"
== "p"
>> parse weapons [some [into [x: abr (print x)] | skip] ]
paper
or
>> parse weapons [collect some [into [x: abr keep (x)] | skip] ]
== ["paper"]
If you want the block starting from what is found, remove index?
switch player-choice [
"r" [index? find weapons "rock"]
"s" [index? find weapons "scissors"]
"p" [index? find weapons "paper"]
"q" ["quit"]
]