Modulus operator, unexpected results

2002-11-22 Thread Poster
Hi, I am having a little trouble with a sub that is using the modulus
operator.

It is called here-within a while loop:
while (my $row = $sth-fetchrow_hashref()) {
count++;
color_rows( $count )
--some other stuff
td bgcolor=$bgcolortable cell value/td
-some other stuff
}
--here is the sub
sub color_rows
{
my $num = @_;
my $bgcolor;

If ( $num % 2 == 0 ) {
$bgcolor=#bde6de;
} else {
$bgcolor=white;
}
return $bgcolor;
}#end sub

I expect that when the value that is passed in $count is even it will
set the row color green, otherwise it will set it to white. What it is
actually doing is setting the value white every time. 

Thanks.

Ramon Hildreth

 
---
[ www.subudusa.org ] [ www.subudseattle.org ]
[ www.ramonred.com ]




Re: Modulus operator, unexpected results

2002-11-22 Thread Sudarshan Raghavan
On Fri, 22 Nov 2002, Poster wrote:

 Hi, I am having a little trouble with a sub that is using the modulus
 operator.
 
 It is called here-within a while loop:
 while (my $row = $sth-fetchrow_hashref()) {
   count++;
   color_rows( $count )
   --some other stuff
   td bgcolor=$bgcolortable cell value/td
   -some other stuff
 }
 --here is the sub
 sub color_rows
 {
 my $num = @_;

This statement assigns the number of elements in @_(no of arguments passed 
into the sub) into $num. In your case $num will always contain 1.

This should be 
my $num = $_[0];
or
my $num = shift;


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




Re: Modulus operator, unexpected results

2002-11-22 Thread Jeff 'japhy' Pinyan
On Nov 22, Poster said:

Hi, I am having a little trouble with a sub that is using the modulus
operator.

No, your function is written incorrectly, and you don't use it correctly.

while (my $row = $sth-fetchrow_hashref()) {
   count++;
   color_rows( $count )
   --some other stuff
   td bgcolor=$bgcolortable cell value/td
   -some other stuff
}

sub color_rows {
my $num = @_;

First mistake is that line.  That assigns the NUMBER of elements in @_ to
$num.  Use one of the following methods:

  my $num = shift;
  # or
  my ($num) = @_;

my $bgcolor;

Your while loop can't see this variable.

if ( $num % 2 == 0 ) {
   $bgcolor=#bde6de;
} else {
   $bgcolor=white;
}
return $bgcolor;
}#end sub

If you're returning a value, USE it:

  while (...) {
my $bg = color_rows($count++);
print qq{td bgcolor=$bg.../td};
  }

There's no need for a function, though.

  while (...) {
my $bg = $count++ % 2 ? #bde6de : #ff;
# ...
  }

-- 
Jeff japhy Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
stu what does y/// stand for?  tenderpuss why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


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