Jeff Westman wrote:

> Hi,
>
> In my limited experience with perl, I've never had to use the 'amp' command,
> even though I see it used all the time.  It seems to just be a short-cut of
> other commands/keywords that I've used.
>
> So, when do you HAVE to use 'map', when no other option makes sense?!
>

When you want to preserve an array, but generate a second one based on some
function of each element.

Greetings! E:\d_drive\ocf\discuss\prototype>perl -w
my @source = (3, 6, 7, 9);
my @target = map $_ + 1, @source;
print "$_ " for @source;
print "\n";
print "$_ " for @target;
print "\n";
^Z
3 6 7 9
4 7 8 10

It should ot be used to modify the elements in the original. You are right--that
is what a for() loop is for.  You should be careful to avoid processes that
affect the default variable, because they can have evil side-effects:

Greetings! E:\d_drive\ocf\discuss\prototype>perl -w
my @source = (3, 6, 7, 9);
my @target = map ++$_, @source;
print "$_ " for @source;
print "\n";
print "$_ " for @target;
print "\n";
^Z
4 7 8 10
4 7 8 10

...or worse.  That was actually my second try at illustrating the problem.  The
first, using the more familiar postfix increment, had even more horrid results:

Greetings! E:\d_drive\ocf\discuss\prototype>perl -w
my @source = (3, 6, 7, 9);
my @target = map $_++, @source;
print "$_ " for @source;
print "\n";
print "$_ " for @target;
print "\n";
^Z
4 7 8 10
3 6 7 9

... as in, the completely wrong array got changed. Tthere are many good uses for
mappings, I can assure you.  They just should not be used for their side effects

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to