On Tue, 30 Sep 2014 14:08:14 -0700
SSC_perl <[email protected]> wrote:
> Is the output of these two lines equivalent?
>
> map { $hash->{$_} = shift @record } @{$self->{'FIELDNAMES'}};
>
> $hash->{$_} = shift @record foreach @{$self->{'FIELDNAMES'}};
[...]
> Is one more appropriate than the other in a situation like
> this, or is it simply a styling difference?
map is more suitable when using it to transform a list; it doesn't make
sense to use it as a flow-control statement where a for loop would be
more sane.
In other words, if you're not assigning or using the result of the map
call, you're using it wrong, and a for loop would be more readable
(and, in older perls at least, likely more efficient).
So, e.g.:
my %fields = map { $_ => shift @record } @{$self->{'FIELDNAMES'}};
... would make sense, because you're using map to produce the data you
want, rather than using it instead of a for loop.
Like most things, TIMTOWTDI, but I believe Perl Best Practices advises
against using map in void context.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/