> -----Original Message-----
> From: Zachary Buckholz [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, June 04, 2002 10:11 PM
> To: [EMAIL PROTECTED]
> Subject: probably a simple question
> 
> 
> Is there any function to tell if a number is even or odd?
> I am looking for an easy way to loop through a list and
> output table cell bgcolor based on even / odd.
> 
> if its an even numbered row make it red if its odd make
> it blue.
> 
> I have done it in the past by turning a switch on or off
> but I think there must be an easier way.

Odd numbers have a least sigificant bit of 1, so just mask
off that bit with bitwise-and:

   $color = $row & 1 ? "blue" : "red";

Parens are a good idea when mixing operators. They aren't
required, since & has a higher precedence than ?:, but
they make the expression more clear:

   $color = ($row & 1) ? "blue" : "red";

If you want a function, just use:

   sub odd { $_[0] & 1 }

   $color = odd($row) ? "blue" : "red";

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to