[prog] Little Perl Question

Katie Bechtold katie at katie-and-rob.org
Mon Sep 30 11:07:04 EST 2002


On Mon, Sep 30, 2002 at 05:55:46AM -0700, Malke Routh wrote:
> $first_number - $second_number
> how do I specify that the first number always has to be bigger than the
> second number?

This is similar to Jenn's answer but based on a slightly different
interpration of what you want to happen:

($first_number, $second_number) = $a > $b ? ($a, $b) : ($b, $a);

This CONDITIONAL ? THEN : ELSE Perl syntax is a carry-over from C.
My example assumes that $a and $b are your two randomly generated
numbers.  The CONDITIONAL part tests whether $a > $b.  If that
expression evaluates to true, $a is assigned to $first_number, and
$b is assigned to $second_number.  If it evaluates to false, $b is
assigned to $first_number and $a is assigned to $second_number.

-- 
Katie Bechtold
http://katie-and-rob.org/




More information about the Programming mailing list