in reply to Having an expression as a hash key

Well, a comparison like $x < $y returns '' for false and 1 for true. However, the comparison $x <=> $y returns -1 if $x is less than $y, 0 if $x is equal to $y, and 1 if $x is greater than $y. This means that you could do:
my %messages = ( -1 => "too low", 0 => "just right", 1 => "too high", ); while (...) { print $messages{ $guess <=> $answer }, "\n"; }
You could also do this as an array:
my @messages = ( "just right", "too high", "too low", ); print $messages[$guess <=> $answer];
although I'd guess several monks here might take offense to that approach.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Having an expression as a hash key
by pKai (Priest) on Dec 03, 2005 at 23:38 UTC
    although I'd guess several monks here might take offense to that approach.
    Do you suggest that <=> wasn't set up the way it is, to be used the way you described? ;-D
Re^2: Having an expression as a hash key
by sub_chick (Hermit) on Dec 04, 2005 at 02:32 UTC
    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)
      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)