in reply to map and grep syntax question
I'm as big a fan of grep and map as the next guy, but sometimes they just don't make sense. In this case, I think a plain for loop would be clearer:
my $parts = [ 'Part1', 'Part2','Part3' ]; my $newLoop; for (@$parts) { my $hash = { PARTNAME => $_ }; $hash->{SELECTED}++ if $_ eq $row->{title}; push @$newLoop, $hash; }
Or, if anything, just a single map call, but this demonstrates why I prefer the for loop:
my $parts = [ 'Part1', 'Part2','Part3' ]; my $newLoop = [ map {{ PARTNAME => $_, $_ eq $row->{title} ? (SELECTED => 1) : () }} @$parts ];
Update: Joost beat me to the punch on the single-map part, but I still prefer my first snippet.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: map and grep syntax question
by Joost (Canon) on Dec 10, 2004 at 22:06 UTC | |
|
Re^2: map and grep syntax question
by geektron (Curate) on Dec 10, 2004 at 22:46 UTC | |
by revdiablo (Prior) on Dec 10, 2004 at 23:17 UTC |