Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've been trying to modify the code from the HTML::Template tutorial Specfically, I'm focusing on the below piece of code:
my $rows = [ map { my $row = $_; (++$i % 2) ? { ODD => [ map { {VALUE => $_} } @{$row} ] } : { EVEN => [ map { {VALUE => $_} } @{$row} ] } } @{$data} ];
I would like to modify the (++$i % 2) line. I have a $results variable and would like to map to the ODD output when the $results var is greater than zero and to the EVEN when the $results var is less than zero. I tried just replacing (++$i % 2) with $results>0) but that didn't work. Any suggestions? Thanks.

Replies are listed 'Best First'.
Re: Mapping Logic
by pg (Canon) on Jan 02, 2003 at 02:56 UTC
    Of course, you can make the change you want. It has nothing to do with map, we are actually talking about the ?: operator.

    my $rows = [ map { my $row = $_; ($results > 0) ? { ODD => [ map { {VALUE => $_} } @{$row} ] } : { EVEN => [ map { {VALUE => $_} } @{$row} ] } } @{$data} ];
    Pure guess, your problem might be where you defined your $results, and how you used it. be careful about one thing,
    (++$i % 2)
    is not just a condition, each iteration, it also alters the value of $i. but
    ($results > 0)
    does not modify $results, unless being modified else where inside the map {}, it stays the same for each iteration, which might not be what you want.

    Show more detail, I can update this post.

    Update:

    Okay, I can see what you are doing. I mimic'd what you were doing, and the result from the attached code make sense to me.

    Pls modify the following code I attached, and add a third section, to show me your problem with $results.
    use Data::Dumper; { my $data = [[0,1,2,3,4,5], [1,2,3,4,5,6], [2,3,4,5,6,7], [3,4,5,6,7,8], [4,5,6,7,8,9], [5,6,7,8,9,10]]; my $i = 0; my $rows = [ map { my $row = $_; (++ $i % 2) ? { ODD => [ map { {VALUE => $_} } @{$row} ] } : { EVEN => [ map { {VALUE => $_} } @{$row} ] } } @{$data} ]; print Dumper($rows); } { my $data = [[0,1,2,3,4,5], [1,2,3,4,5,-6], [2,3,4,5,6,7], [3,4,5,6,7,8], [4,5,6,7,8,9], [5,6,7,8,9,10]]; my $rows = [ map { my $row = $_; (@{$row}[5]>= 0) ? { ODD => [ map { {VALUE => $_} } @{$row} ] } : { EVEN => [ map { {VALUE => $_} } @{$row} ] } } @{$data} ]; print Dumper($rows); }
    The result is:
    $VAR1 = [ { 'ODD' => [ { 'VALUE' => 0 }, { 'VALUE' => 1 }, { 'VALUE' => 2 }, { 'VALUE' => 3 }, { 'VALUE' => 4 }, { 'VALUE' => 5 } ] }, { 'EVEN' => [ { 'VALUE' => 1 }, { 'VALUE' => 2 }, { 'VALUE' => 3 }, { 'VALUE' => 4 }, { 'VALUE' => 5 }, { 'VALUE' => 6 } ] }, { 'ODD' => [ { 'VALUE' => 2 }, { 'VALUE' => 3 }, { 'VALUE' => 4 }, { 'VALUE' => 5 }, { 'VALUE' => 6 }, { 'VALUE' => 7 } ] }, { 'EVEN' => [ { 'VALUE' => 3 }, { 'VALUE' => 4 }, { 'VALUE' => 5 }, { 'VALUE' => 6 }, { 'VALUE' => 7 }, { 'VALUE' => 8 } ] }, { 'ODD' => [ { 'VALUE' => 4 }, { 'VALUE' => 5 }, { 'VALUE' => 6 }, { 'VALUE' => 7 }, { 'VALUE' => 8 }, { 'VALUE' => 9 } ] }, { 'EVEN' => [ { 'VALUE' => 5 }, { 'VALUE' => 6 }, { 'VALUE' => 7 }, { 'VALUE' => 8 }, { 'VALUE' => 9 }, { 'VALUE' => 10 } ] } ]; $VAR1 = [ { 'ODD' => [ { 'VALUE' => 0 }, { 'VALUE' => 1 }, { 'VALUE' => 2 }, { 'VALUE' => 3 }, { 'VALUE' => 4 }, { 'VALUE' => 5 } ] }, { 'EVEN' => [ { 'VALUE' => 1 }, { 'VALUE' => 2 }, { 'VALUE' => 3 }, { 'VALUE' => 4 }, { 'VALUE' => 5 }, { 'VALUE' => -6 } ] }, { 'ODD' => [ { 'VALUE' => 2 }, { 'VALUE' => 3 }, { 'VALUE' => 4 }, { 'VALUE' => 5 }, { 'VALUE' => 6 }, { 'VALUE' => 7 } ] }, { 'ODD' => [ { 'VALUE' => 3 }, { 'VALUE' => 4 }, { 'VALUE' => 5 }, { 'VALUE' => 6 }, { 'VALUE' => 7 }, { 'VALUE' => 8 } ] }, { 'ODD' => [ { 'VALUE' => 4 }, { 'VALUE' => 5 }, { 'VALUE' => 6 }, { 'VALUE' => 7 }, { 'VALUE' => 8 }, { 'VALUE' => 9 } ] }, { 'ODD' => [ { 'VALUE' => 5 }, { 'VALUE' => 6 }, { 'VALUE' => 7 }, { 'VALUE' => 8 }, { 'VALUE' => 9 }, { 'VALUE' => 10 } ] } ];
      This is exactly how I have it defined:
      my $rows = [ map { my $row = $_; (@{$row}[5]>= 0) ? { ODD => [ map { {VALUE => $_} } @{$row} ] } : { EVEN => [ map { {VALUE => $_} } @{$row} ] } } @{$data} ];
      Using the code above, the rows are correctly highlighted according to whether or not the 5th item is greater or less than zero. However only about half of the data is printed out. When I sub the (++$i % 2) back into the code, all of the data is printed out. Any idea why?
Re: Mapping Logic
by dws (Chancellor) on Jan 02, 2003 at 04:00 UTC
    I've been trying to modify the code from the HTML::Template tutorial.

    Did you notice the "Miscelaneous Options" section in the HTML::Template POD? If you set   loop_context_vars => 1 when you create the HTML::Parser instance, you'll get a couple of magic variables that you can use inside of <TMPL_LOOP> for doing odd/even stuff. It lets you write

    <TMPL_LOOP stuff> <TMPL_IF NAME="__ODD__"> Odd stuff <TMPL_ELSE> Even stuff. </TMPL_IF> </TMPL_LOOP>
    Depending on what you're doing, handling odd/even in the template can be a lot cleaner than trying to do it in code.