Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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.my $rows = [ map { my $row = $_; (++$i % 2) ? { ODD => [ map { {VALUE => $_} } @{$row} ] } : { EVEN => [ map { {VALUE => $_} } @{$row} ] } } @{$data} ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Mapping Logic
by pg (Canon) on Jan 02, 2003 at 02:56 UTC | |
Pure guess, your problem might be where you defined your $results, and how you used it. be careful about one thing, is not just a condition, each iteration, it also alters the value of $i. but 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. The result is:
| [reply] [d/l] [select] |
by Anonymous Monk on Jan 02, 2003 at 03:27 UTC | |
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? | [reply] [d/l] [select] |
|
Re: Mapping Logic
by dws (Chancellor) on Jan 02, 2003 at 04:00 UTC | |
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 Depending on what you're doing, handling odd/even in the template can be a lot cleaner than trying to do it in code.
| [reply] [d/l] [select] |