in reply to Number gussing game
If you change your result constants to -1, 0 and 1 then you can change your test code to:
$result = $guess <=> $target; $min = $guess if $result == -1; $max = $guess if $result == 1;
which uses the numeric comparison operator <=> (or spaceship, see 'Equality Operators' in perlop) to perform the check. You need to change the while loop test of course.
Note too the use of if as a statement modifier to make the code a little clearer.
$guess and $interval should be declared within the loop to make it more obvious that their values are not used outside the loop. You should almost never use variables global to a sub inside the sub - pass them in as parameters.
|
|---|