CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Should be easy question finding odd & even integer

 



ARSONe
Deleted

Jan 1, 2001, 5:11 PM

Post #1 of 5 (612 views)
Should be easy question finding odd & even integer Can't Post

I have a flat file DB and im making a script to display the data in tables.. i already have this done. One of the fields in the flat file DB is a ID# starting at 1 with no planned end. I want to make the table background color alternate every row. I thought the eaiest way to do this would be take the ID #, if its odd give i one color if its even give it another and have a if conditional statement sort it out for me. BTW the newest (highest number) is listed first at top.

So the question is: How can i find out if the number is odd or even and is there a more simple way i just havent thought of?



Lawrence
Novice

Jan 1, 2001, 9:50 PM

Post #2 of 5 (606 views)
Re: Should be easy question finding odd & even integer [In reply to] Can't Post

Just an if statement like below would work (untested). If $num is the variable with the number in it:

if ($num / 2 == int ($num / 2)) {
# $num is even
}
else {
# $num is odd
}

If $num is odd (say 7) then $num / 2 = 3.5 but int ($num / 2) = 3, so the if statement returns false. If $num is even (say 8) then $num / 2 = 4 and in ($num / 2) = 4, so the if statement returns true.

Hope that helps,

- Lawrence




Jasmine
Administrator

Jan 2, 2001, 8:42 AM

Post #3 of 5 (602 views)
Re: Should be easy question finding odd & even integer [In reply to] Can't Post

This is the exact code I use to generate alternate table row colors:


Code
   
my $count;
foreach (@data){
my $trcolor = ($count++ % 2) ? $color1 : $color2;
# more stuff here
}

The % means it's looking for the modulus (remainder) of the division between the count and 2. If there's a modulus, it's an odd number; if there isn't a modulus, it's an even number.

Combine that with a conditional ? : and $trcolor is assigned based on whether or not $count is even or odd.

Hope this helps!



rGeoffrey
User / Moderator

Jan 2, 2001, 9:03 AM

Post #4 of 5 (602 views)
Re: Should be easy question finding odd & even integer [In reply to] Can't Post

Here is one possible solution to alternate colors.

Somewhere in a module, or in the main script before you want to use it, place this block of code.


Code
my %ALTERNATING_COLORS = (ffffcc => 'ffccff', 
ffccff => 'ccffff',
ccffff => 'ffffcc');
my $ALTERNETING_CURRENT = 'ffffcc';

sub Alternate_Color
{
$ALTERNETING_CURRENT = $ALTERNATING_COLORS{$ALTERNETING_CURRENT};
return ($ALTERNETING_CURRENT);
}

It needs to be done before you use it because it uses two global variables and they need to exist.

Then when you want to print a table you can say something like...


Code
my @rows = ('<td>Fred</td><td>Wilma</td>', 
'<td>Barney</td><td>Betty</td>',
'<td>Homer</td><td>Marge</td>',
'<td>George</td><td>Jane</td>');

print "<table>\n";

foreach @rows {
print '<tr bgcolor="#', &Alternate_Color (), '">', $_, "</tr>\n";
}

print "</table>\n";

Each time you call &Alternate_Color it will look in the hash to grab the next color in line and return it. This hash is set for three colors, but it can be any number of colors as long as each key is a value exactly once in the hash.



zanardi
journeyman

Feb 5, 2001, 1:58 PM

Post #5 of 5 (573 views)
Re: Should be easy question finding odd & even integer [In reply to] Can't Post


Code
foreach @rows { 
print '<tr bgcolor="#', &Alternate_Color (), '">', $_, "</tr>\n";
}

dont you think you kinda did that the hard way? all those comma's are confusing :) but I would've done it this way:


Code
foreach @rows {    
$AltColor = Alternate_Color();
print qq(<tr bgcolor="#$AltColor">$_</tr>\n);
}

----------------------
Fueled By ZCom (beta 2 stage)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives