如何查找第一个字符与输入字符匹配的字符串块的第一个元素?

时间:2019-01-20 01:57:31

标签: red

给出weapons: ["rock" "scissors" "paper"]

如果我做了player-choice: ask "(r)ock, (p)aper, (s)cissors or (q)uit? "

我如何查找用户在块中附加了单词weapons的字符

4 个答案:

答案 0 :(得分:2)

如果您只想要一个匹配项,并且仅在块中使用实际的项目名称,那么您自己的解决方案就可以了。但是关于Red的重要一件事是如何构造数据以使事情变得更容易。例如,如果您想仅基于已知键(例如第一个字符)从列表中选择项目,则可以将其设为显式而不是隐式。

weapons: ["r" "rock" "s" "scissors" "p" "paper"]
player-choice: ask "(r)ock, (p)aper, (s)cissors or (q)uit? "
print select weapons player-choice

答案 1 :(得分:1)

weapons: ["rock" "scissors" "paper"]
matching-weapon: func [abbrev][
    foreach weapon weapons [
        if (first weapon) = first abbrev [
            return weapon
        ]
    ]
]

答案 2 :(得分:1)

>> abr: "p"
== "p"
>> parse weapons [some [into [x: abr (print x)] | skip]  ]
paper

>> parse weapons [collect some [into [x: abr keep (x)] | skip]  ]
== ["paper"]

答案 3 :(得分:-1)

如果要从找到的内容开始,请删除index?

switch player-choice [
    "r" [index? find weapons "rock"]
    "s" [index? find weapons "scissors"]
    "p" [index? find weapons "paper"]
    "q" ["quit"]
]