2012-10-24 29 views
0

這似乎是一個奇怪的問題。我已經得到了一些帶有輸入字符串的Ruby代碼,它使用scanflatten來提取特定的值,然後希望在if...then聲明中使用該值,但我遇到了問題。評估Ruby正則表達式和變量

我的輸入字符串描述了區域中'危險的生物'的數量。當描述沒有危險的生物或兩個或更多的生物時,字符串總是標準的,例如:

「區域內沒有危險的生物」或「一個危險的生物......」等等。

我用這個來取代代表生物數量的詞:「no」,「one」,「two」等,希望稍後將它們轉換爲數值 - 「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變量,在這個例子中,我得到了「no」。

當我通過我的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。我希望有人看着這個,去'呃,試試這個。'

根據要求這裏是所有的代碼,有來電在這裏等功能,可能沒有什麼意義......

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 
+0

請出示您遇到問題的確切代碼。 –

+0

我一直希望避免這種情況,因爲在它的範圍之外有對Ruby函數的調用,但我會將其添加到主要問題中。 – user401093

+0

你在正則表達式中有什麼'a *'?這似乎是你的問題的原因之一。 – sawa

回答

0

的問題是,crittercount是一個數組,而不是字符串。因此,if crittercount == "no"等將永遠不會是true,你將永遠在最後的else

解決辦法是你的代碼更改爲:

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

有了這個crittercount將是一個字符串或nil和你的代碼會正常運行的其餘部分。

+0

這樣做。非常感謝。 – user401093

+0

不客氣。我的回答只是顯示*爲什麼*你的代碼沒有工作。這並不意味着這是最好的解決方案。其他答案有很好的建議來大幅縮短你的代碼。你不需要所有那些「如果...結束」。 – Mischa

0

它看起來像你正在做...如果...結束如果...結束如果...結束而不是如果... elsif ..elsif ...結束

因此,對於超過10以外的任何值,你會在此結束了封鎖其他

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

case語句會在這裏好......

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] 
1

scan當您可能預期多個匹配時很有用。既然你只希望每個字符串匹配一次,你就不應該使用它。

以下將直接給你critters。你不需要所有這些條件。

regex = /(\bno\b)|(\bone\b)|(\btwo\b)|(\bthree\b)|(\bfour\b)|(\bfive\b) 
      |(\bsix\b)|(\bseven\b)|(\beight\b)|(\bnine\b)|(\bten\b)/x 

critters = strcheck.match(regex).to_a.drop(1).index{|x| x} || 99 
0

掃描返回一個數組,包含和匹配數據數組,所以在你的代碼中如果你調用.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}" 
使用的情況下清理邏輯

,只是使用正則表達式匹配和提取第一場比賽