On 09/30/2014 05:08 PM, SSC_perl wrote:
Is the output of these two lines equivalent?
map { $hash->{$_} = shift @record } @{$self->{'FIELDNAMES'}};
$hash->{$_} = shift @record foreach @{$self->{'FIELDNAMES'}};
They appear to be in my testing, but I'd like to make sure.
Is one more appropriate than the other in a situation like this, or is
it simply a styling difference?
both do the exact same thing. the general rule is to use map only when
you are generating a new list which is the output of map. your code is
called using map in a void context. perl used to generate the list and
then throw it out which is a waste. even with that inefficiency fixed,
it is still considered by most a poor coding style. you are telling the
reader that you are building a list but don't use it.
the foreach is better style yet some might not like the shift trick (i
have used that one). but you just are assigning a list of values to a
list of tokens in a hash. a slice is the best and fastest way to do that.
my %hash ;
@hash{ @{$self->{'FIELDNAMES'} } = @record ;
done!
if you are using DBI, you can get the row back as a hash reference and
not need that code.
uri
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/