i was trying to get a combination of map and grep working to set a selected element to pass to HTML::Template without using a foreach loop.
Um, why? What's wrong with foreach loops?
In my opinion, code like this:
my $parts = [ 'Part1', 'Part2','Part3' ]; my $newLoop = [ grep { $_->{SELECTED}++ if $_->{PARTNAME} eq $row->{title}, $_ } map +{ 'PARTNAME' => $_ }, @$parts ] ;
is bad even if it works, which--as you found out--it doesn't. grep/map are great as list filters, when you want to build one list from another, but they force you to stuff all of your code in one tiny block/expression. foreach is the general-purpose list iterator, and you should always be ready to fallback on it if grep or map get too hairy, like revdiablo showed you:
for (@$parts) { my $hash = { PARTNAME => $_ }; $hash->{SELECTED}++ if $_ eq $row->{title}; push @$newLoop, $hash; }
foreach is your friend.
UPDATE: -2? What was so objectionable about suggesting Perl programmers use a clearer construct over a shorter one?
In reply to Re: "advanced" Perl functions and maintainability
by William G. Davis
in thread "advanced" Perl functions and maintainability
by geektron
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |