in reply to Re: Having an expression as a hash key
in thread Having an expression as a hash key

In response to your solution, Japhy...I called on the help of a friend to try and help me decode it.
print $messages{$guess <=> $answer}, "\n"; that's mean there are processes, and the processes are :

1. firstly, a comparison between $guess and $answer. $answer isnt defined yet so
$answer = 0. Looking back at $guess = 0, $answer= 0, 0 <=> 0 (zero compare zero) will return 0.

2. $guess <=> $answer return 0 that's mean we we're returning '0' to $guess, then look at this $messages{$guess} alias $messages{0}.

3. what's the value of $messages{0} and we print it, "print $message{$guess} aka print $message{$guess} alias "just right"

So, is this grasping the concept behind $messages{$guess <=> $answer}? which is the part I am mostly trying to understand.

"Es gibt mehr zu Leben als Bücher, kennen Sie. Aber nicht viel mehr " -(Der Smiths)

Replies are listed 'Best First'.
Re^3: Having an expression as a hash key
by McDarren (Abbot) on Dec 04, 2005 at 08:39 UTC
    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:

    $messages{-1}
    So.... print $messages{ $guess <=> $answer }, "\n"; in this instance will print "too low".

    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.

      Ahh, thank you McDarren for clearing that up. I think i can now use what japhy said and update my code.

      "Es gibt mehr zu Leben als Bücher, kennen Sie. Aber nicht viel mehr " -(Der Smiths)