Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    set of eyes required plz.

    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

    Code:
       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>
        );
      
      }
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    This line is not doing what you expect.
    Code:
    my @even_numbers = '2,4,6,8,10';

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Instead of using the array and if/else block, I'd use the modulus operator.
    Code:
    my $row_shading = $count_the_rows % 2 ? "shaded" : '';
    Last edited by FishMonger; 12-23-2009 at 02:31 PM.

  • #5
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Great FishMonger; thanks.

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

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •