...

View Full Version : set of eyes required plz.



bazz
12-23-2009, 03:00 PM
Hi,

within a loop, I am trying to see if a $count value is in an array. If so, the css should be of one sort and if not, it should ne another.

The grep seems not to make the difference and so the first row is printing out with the css of 'shaded' but the rest aren't. I can't see what is wrong and I would appreciate if one of you can point me to my error.

bazz



my $count_the_rows=0;
my @even_numbers = '2,4,6,8,10';

foreach my $order_no ( sort keys %cats ) {
$count_the_rows++;
my $row_shading;

if ( grep /$count_the_rows/ , @even_numbers) {
$row_shading = "shaded";
}
else{
$row_shading ='';
}

my $cat = $cats{$order_no};

print qq(
<tr>
<td class='$row_shading category'>$cat $count_the_rows</td>
<td class='$row_shading'><input type='radio' name="$cat" value='1'/></td>
<td class='$row_shading'><input type='radio' name="$cat" value='2'/></td>
<td class='$row_shading'><input type='radio' name="$cat" value='3'/></td>
<td class='$row_shading'><input type='radio' name="$cat" value='4'/></td>
<td class='$row_shading'><input type='radio' name="$cat" value='5'/></td>
</tr>
);

}

bazz
12-23-2009, 03:16 PM
OK, must have had part of the probelm being caused by a caching issue.

However, if I maintina 2 4 6 8 10 in my array, the grep seems to take the 1 from the ten as being 1 and assigning the shading to the 1st row. removing 10 fcrom ther arreay 'fixes' that but I want it to have 10 in it.

So, how do I have a 10 in the array but grep not to read it as 1?

bazz

FishMonger
12-23-2009, 03:20 PM
This line is not doing what you expect.

my @even_numbers = '2,4,6,8,10';

FishMonger
12-23-2009, 03:28 PM
Instead of using the array and if/else block, I'd use the modulus operator.

my $row_shading = $count_the_rows % 2 ? "shaded" : '';

bazz
12-23-2009, 05:44 PM
Great FishMonger; thanks.

That works better by making 1,3, 5 etc shaded. I must read up on modulus operators.

bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum