in reply to Re: "advanced" Perl functions and maintainability
in thread "advanced" Perl functions and maintainability
Well, this problem is to build one list from another. Your example builds one list from another. To be clear though: grep is a filter, but map is a transformer.
Neither map nor grep force you to do stuff anything anywhere. There is no magic rule that says it has to fit on one line. You're doing all that work in the foreach anyway (although you're stuffing much more stuff into it).
Consider what is important in the problem. Is it iterating through each element and doing something, or is it creating a new list? In this case, it sure looks like the new list is the point of the exercise, so it should get top billing. When you use map, you emphasize the new list. When you use foreach, you bury the list creation in a bunch of other code. The new list doesn't stand out. Using foreach in this instance may make the syntax readable to anyone with a week's worth of Perl under their belt, but it doesn't make the program logic more readable.
@$newLoop = map { my $hash = { PARTNAME => $_ }; $hash->{SELECTED}++ if $_ eq $row->{title}; $hash; } @$parts;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: "advanced" Perl functions and maintainability
by William G. Davis (Friar) on Dec 13, 2004 at 03:19 UTC | |
by Anonymous Monk on Dec 13, 2004 at 10:46 UTC | |
by William G. Davis (Friar) on Dec 13, 2004 at 12:58 UTC | |
by Anonymous Monk on Dec 13, 2004 at 13:49 UTC | |
by William G. Davis (Friar) on Dec 13, 2004 at 14:33 UTC | |
|