in reply to Mapping Logic

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 } ] } ];

Replies are listed 'Best First'.
Re: Re: Mapping Logic
by Anonymous Monk on Jan 02, 2003 at 03:27 UTC
    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?