Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Hash of arrays generation

by nemesisgus (Acolyte)
on Sep 04, 2012 at 17:15 UTC ( #991661=perlquestion: print w/replies, xml ) Need Help??
nemesisgus has asked for the wisdom of the Perl Monks concerning the following question:

Sorry for the noob question but I couldn't find the answer in existing threads:

I think the common way to generate a Hash of Arrays is using square brackets, something like:

$HoA{$key} = [ @fields ]

but this also works:

@{ $HoA{$key} } = @fields

Are those exactly the same? Is the first preferred over the second for any reason?

I know the second form is used to add elements to the array using push, for instance:

push @{ $HoA{$key} }, @more_fields

Thank you.

Replies are listed 'Best First'.
Re: Hash of arrays generation
by SuicideJunkie (Vicar) on Sep 04, 2012 at 18:17 UTC

    The former will make $HoA{$key} an array ref regardless of what it was before.

    The latter will cause problems if $HoA{$key} was something other than an arrayref.

    use strict; use warnings; my %HoA; $HoA{'bar'} = 'baz'; @{$HoA{'foo'}} = (1,2,3); @{$HoA{'bar'}} = (1,2,3);
    Gives: Can't use string ("baz") as an ARRAY ref while "strict refs" in use at line 6.

      Thank you both for answering.

      I didn't consider the case of an already defined key element. Thanks for pointing it out!

Re: Hash of arrays generation
by GrandFather (Sage) on Sep 05, 2012 at 01:47 UTC

    I'd use:

    $HoA{$key} = \@fields;

    unless I really wanted to copy the elements in @fields. Most often I'd do that sort of thing in a loop where @fields is local to the loop and the assignment is performed at the end of the loop block.

    The trap with either of variants you show is that only a shallow copy is made. If @fields contains nested elements then the references are copied, not the nested data. Using \@fields makes it just a little clearer (at least to me) in that case that danger lurks under the surface because it is clearer that there isn't much copying going on.

    True laziness is hard work
      Good point, thanks for the advice GrandFather.
Re: Hash of arrays generation
by VinsWorldcom (Parson) on Sep 04, 2012 at 17:26 UTC

    Maybe a more perl-guts competent poster can comment on the technical benefits of one versus the other, but I see this as the Perl motto: TIMTOWTDI.

    Personally, I'd use them exactly as you describe. The first (to me) is more readable as to what I'm doing and I'd use it to assign a single value. I'd use the second form in the exact use case you point out - 'push' values to the existing structure.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991661]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-05-20 14:12 GMT
Find Nodes?
    Voting Booth?