2012-10-24 3 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 crittercount == "ten" 
    critters = 10 
else 
    critters = 99 
end 

이 문제가 사용 crittercount == "ten"

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

(나는이 문제를 알아 낸 얻을 때 나는 if..elsif..end를 사용합니다) : 나는 10에 동일한 maxcreatures라는 변수가 있습니다. 예를 들어 strcheck 값을 "위험한 생물 없음"으로 지정하면 정확한 문자열을 반환하는 값을 출력합니다. 그런 다음 crittercount 변수를 인쇄합니다.이 예제에서는 "no"를 얻습니다. 그래도 난 내 if..then.. 문을 통과 할 때

, 내가 사용하는 무엇을 평가 : 나는 99지고있어 모든 상황에서

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 

을하고 나는 모든 것을 시도했습니다 맹세합니다. 정규식 끝에 .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이됩니다. 문자열이나 nil 예상대로 작동 코드의 나머지 부분이 될 것입니다이 crittercount으로

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

:

솔루션에 코드를 변경하는 것입니다.

+0

그랬습니다. 감사합니다. – user401093

+0

당신을 진심으로 환영합니다. 내 대답은 * 왜 * 코드가 작동하지 않는지를 보여줍니다. 이것이 이것이 최선의 해결책이라는 것을 의미하지는 않습니다. 다른 답변에는 코드를 대폭 단축 할 수있는 좋은 제안이 있습니다. 당신은'if ... end'을 모두 필요로하지 않습니다. – Mischa

0

만약 ... if end ... if end ... if ... if ... if ... if ... ifif ... else ...... (10) 이외의 값에 따라서

, 당신은 다른 사례 문은 여기에 더 좋을 것이다

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

scan은 일치하는 데이터가 포함 된 배열을 반환하므로 코드에서 .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}" 

그런 짓을하고 바로 첫 경기

관련 문제