This will seem like an odd problem to have. I've got this bit of Ruby code that takes an input string, uses scan and flatten to extract a particular value, and the hope is to then operate off that value in an if...then statement, but I'm having issues.
My input string describes the number of 'dangerous creatures' in the area. The string, when describing no dangerous creatures or two or more creatures is always standard, something like:
"no dangerous creatures in the area" or "one dangerous creature..." and so on.
I'm using this to fetch the word representing the number of creatures as: "no", "one", "two", and so on in hopes of converting these to numeric values later on - "no" = 0, "one" = 1, etc.
To do this, I use:
crittercount = strcheck.scan(/a*(no|one|two|three|four|five|six|seven|eight|nine|ten)a*/)
Then I go and do an if then on the variable crittercount saying:
if crittercount == "no"
critters = 0
... exit and go do something with the var...
end
I do this for each one. (when I get this issue figured out I'll use if..elsif..end)
if crittercount == "one"
critters = 1
... exit and go do something with the var...
end
...
After crittercount == "ten" I just use
if crittercount == "ten"
critters = 10
else
critters = 99
end
Here is the problem: I have a variable called maxcreatures equal to 10. I take the strcheck value of "no dangerous creatures in the area" for example, I print out the value it returns that exact string. Then I print out the crittercount variable, in this example, I get "no".
When I get through my if..then.. statements though, I evaluate what to do using:
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
In every situation I'm getting 99 and I swear I've tried everything. I tried using .flatten at the end of the regex, I tried using .strip on the crittercount var. I'm hoping someone looks at this and goes 'duh, try this.'
On request here is all the code, there are calls in here to other functions that may not make sense...
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
a*that you have in the regex? That seems to be part of the reason for your problem.