评估Ruby Regex和变量



这似乎是一个奇怪的问题。我有这样的Ruby代码,它采用输入字符串,使用scanflatten提取特定值,希望然后在if...then语句中运行该值,但是我有问题。

我的输入字符串描述了该区域中的"危险生物"的数量。弦,当描述没有危险生物或两个或两个以上的生物时,总是标准的,例如:

"该地区没有危险生物"或"一个危险的生物……"等等。

我正在使用它来获取表示生物数量的单词:" no","一个","两个",依此类推,希望以后将它们转换为数值值,以后 - " no" = 0,"一个" = 1,等等

为此,我使用:

crittercount = strcheck.scan(/a*(no|one|two|three|four|five|six|seven|eight|nine|ten)a*/)

然后我去变量crittercount上进行if then说:

if crittercount == "no"
  critters = 0
  ... exit and go do something with the var...
end

我每个人都这样做。(当我发现这个问题时,我将使用if..elsif..end

if crittercount == "one"
  critters = 1
  ... exit and go do something with the var...
end
...

crittercount == "ten"之后,我只使用

if crittercount == "ten"
  critters = 10
else
  critters = 99
end

这是问题:我有一个称为maxcreatures的变量等于10。例如,我以"该区域中无危险生物"的strcheck值打印出它返回该精确字符串的值。然后,我打印出CritterCount变量,在此示例中,我得到"否"。

当我通过if..then..语句时,我会评估该怎么做:

if critters > maxcreatures
  print "Maximum Creatures " + critters.to_s + " and maximum is #{maxcreatures}.  Lets Bail"
else
  print "Critter count " + critters.to_s  + " is less than #{maxcritters} Keep going."
end

在每种情况下,我都会得到99,并且发誓我尝试了所有事情。我尝试在正则末端使用.flatten,尝试在crittercount var上使用.strip。我希望有人看着这个,然后去" du,尝试一下。'

根据要求,所有代码都在这里呼叫其他可能没有意义的功能...

maxcritters = 2
critters = 0

put "count critter"
strcheck = matchfind "You notice ?"
crittercount = strcheck.scan(/a*(no|one|two|three|four|five|six|seven|eight|nine|ten)a*/)
echo strcheck
echo crittercount
crittercount = crittercount.strip
if crittercount == "no"
    critters = 0
    goto "roundup"
end
if crittercount  == "one"
    critters = 1
    goto "roundup"
end
if crittercount == "two"
    critters = 2
    goto "roundup"
end
if crittercount == "three"
    critters = 3
    goto "roundup"
end
if crittercount == "four"
    critters = 4
    goto "roundup"
end
if crittercount == "five"
    critters = 5
    goto "roundup"
end
if crittercount == "six"
    critters = 6
    goto "roundup"
end
if crittercount == "seven"
    critters = 7
else
critters = 99
end
roundup:
if critters > maxcritters
echo "Maximum Creatures " + critters.to_s + " and maximum is #{maxcritters}.  Lets Bail"
critters == nil
fput "retreat"
else
    echo "Critter count " + critters.to_s  + " is below maximum of #{maxcritters} - We're cool.  Keep going."
    critters == nil
    goto "combatcheck"
end

scan在您可能期望多个匹配时很有用。由于您只期望每个字符串一个匹配项,因此不应使用它。

以下将直接给您critters。您不需要所有这些条件。

regex = /(bnob)|(boneb)|(btwob)|(bthreeb)|(bfourb)|(bfiveb)
            |(bsixb)|(bsevenb)|(beightb)|(bnineb)|(btenb)/x
critters = strcheck.match(regex).to_a.drop(1).index{|x| x} || 99

问题是 crittercount是一个数组,而不是字符串。因此,if crittercount == "no"等永远不会是true,您将始终进入最后一个else

解决方案是将您的代码更改为:

crittercount = strcheck.scan(/no|one|two|three|four|five|six|seven|eight|nine|ten/).first

使用此crittercount是字符串或nil,其余代码将按预期工作。

扫描返回一个数组,其中包含和数组匹配数据,因此,如果您称为.first.first,则在您的代码中,您将在第一场比赛中获得。您也可以做这样的事情

strcheck = "two dangerous creatures in the area" # or "one dangerous creature...'
strcheck =~ (/(.*) dangerous.*/)
critters = case $1 
  when 'no' then 0
  when 'one' then 1
  when 'two' then 2
  when 'three' then 3
  when 'four' then 4
  when 'five' then 5
  when 'six' then 6
  when 'seven' then 7
  else 99 
end
puts "number of critters #{critters}"

使用案例清理逻辑,只需使用正则匹配和提取第一个匹配

看起来您在做……如果...结束...结束...结束而不是...

因此,对于10以外的任何值,您将最终进入此else Block

if crittercount == "ten"
  critters = 10
else
  critters = 99
end

案例语句在这里更好...

case (crittercount)
    when "no" then critters = 0
    when "one" then critters = 1
    when "two" then critters = 2
    when "three" then critters = 3
    when "four" then critters = 4
    when "five" then critters = 5
    when "six" then critters = 6
    when "seven" then critters = 7
    when "eight" then critters = 8
    when "nine" then critters = 9
    when "ten" then critters = 10
    else critters = 99
end

甚至更好,如果查找失败,则默认为99的所有数字查找。

numberHash = { "no" => 0, "one" => 1, "two" => 2, "three" => 3, "four" => 4, "five" => 5, "six" => 6, "seven" => 7, "eight" => 8, "nine" => 9, "ten" => 10 }
numberHash.default = 99
critters = numberHash[critterCount]

最新更新