sub_chick has asked for the wisdom of the Perl Monks concerning the following question:

I was reading through a beginner's perl book that has a very bulky style of writing code. So, I've been trying to develop a certain style of dealing with conditionals and i came across a question I can't seem to find an answer to: Is it possible to write up a hash key that is but a simple expressing using the while loop below?
while () { print "Pick a number between 1 and $top: "; chomp($guess = <>); if ($guess == 0 || $guess eq '0'){ print "Not a suitable number.\n";} elsif ($guess < $num) { print "Too Low!\n";$count++;} elsif ($guess > $num) { print "Too high!\n"; $count++;} else { print "\a\aW00t W00t! You got it!\n"; $count++;last;} }
With this i wondered if it was possible to take the test($guess < $num) and write up a hash instead to look something like %guess = ($guess < $num) => "Too Low!\n";$count++,
Or is this not possible? **Full code can be found in sketchpad

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

Replies are listed 'Best First'.
Re: Having an expression as a hash key
by japhy (Canon) on Dec 03, 2005 at 21:36 UTC
    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
      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
      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.