in reply to Re^2: Having an expression as a hash key
in thread Having an expression as a hash key
So, is this grasping the concept behind $messages{$guess <=> $answer}? which is the part I am mostly trying to understand.
hmmm.. I don't really follow your interpretation. Here is how I would explain it:
Take the result of the numerical comparison between $guess and $answer, and use that as a key to the %messages hash, then return the value associated with that key.
So, for example:
Lets say the correct answer is 10, and the user guesses 5.
Then we effectively have:
$messages{5 <=> 10}
Because 5 is less than 10, 5 <=> 10 will return -1. So that becomes:
So.... print $messages{ $guess <=> $answer }, "\n"; in this instance will print "too low".$messages{-1}
Hope this helps,
Darren :)
PS. The spaceship operator "<=>" is generally used for sorting operations. For a good introduction to sorting, I'd thoroughly recommend this article by merlyn.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Having an expression as a hash key
by sub_chick (Hermit) on Dec 04, 2005 at 13:35 UTC |