iKnowNothing has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have some data that I would like to store in a hash of arrays, however this has proved to be a little complicated for me. I have written some test code below to investigate the behavior of this particular data type, and would like some help trying to understand it.
%Hash = ( ArrayInHash => [1,2,3,4], ); print "Array in hash: @$Hash{ArrayInHash}"; @Array = @$Hash{ArrayInHash}; print "\nArray: @Array"; $ArrayRef = $Hash{ArrayInHash}; @Array2 = @$ArrayRef; print "\nArray from ordinary scalar reference: @Array2"; print "\n\nTrying a foreach loop with the hash"; foreach(@$Hash{ArrayInHash}) { print "\n$_"; } print "\n\nTrying a foreach loop with the scalar array reference"; foreach(@$ArrayRef) { print "\n$_"; }
This produces:
Array in hash: Array: Array from ordinary scalar reference: 1 2 3 4 Trying a foreach loop with the hash Trying a foreach loop with the scalar array reference 1 2 3 4
Why didn't the first two print statement or foreach loop act on the array? There's got to be a better way to access the array besides setting the reference to an ordinary scalar first. Thanks

Replies are listed 'Best First'.
Re: How to use hash of arrays
by saskaqueer (Friar) on Feb 15, 2005 at 21:24 UTC

    You are close, but off by a little. The code @$Hash{ArrayInHash} translates into @{$Hash}{ArrayInHash}, which is definitely not what you want; what you need is @{ $Hash{ArrayInHash} }. So you'd update your code as follows:

    %Hash = ( ArrayInHash => [1,2,3,4], ); print "Array in hash: @{$Hash{ArrayInHash}}"; @Array = @{$Hash{ArrayInHash}}; print "\nArray: @Array"; $ArrayRef = $Hash{ArrayInHash}; @Array2 = @$ArrayRef; print "\nArray from ordinary scalar reference: @Array2"; print "\n\nTrying a foreach loop with the hash"; foreach(@{$Hash{ArrayInHash}}) { print "\n$_"; } print "\n\nTrying a foreach loop with the scalar array reference"; foreach(@$ArrayRef) { print "\n$_"; }
      Awesome! Thanks for the help!
Re: How to use hash of arrays
by Animator (Hermit) on Feb 15, 2005 at 21:17 UTC

    I suggest you take a look at `perldoc perlreftut`, the example they use is exactly the same as your (except the data ofc).

    What goes wrong with @$Hash{ArrayInHash} is that Perl evalutes it as being: @{$Hash}{ArrayInHash} which means that it will try to dereference $Hash to an array, and then access the key 'ArrayInHash' in that array (which does not make sense as you can see).

    What you should use: @{ $Hash{ArrayInHash} } (the whitespace is not required)

Re: How to use hash of arrays
by sh1tn (Priest) on Feb 15, 2005 at 21:18 UTC
    %Hash = ( ArrayInHash => [1,2,3,4] ); print "Array in hash: @{$Hash{ArrayInHash}}"; @Array = @{$Hash{ArrayInHash}}; print "\nArray: @Array"; $ArrayRef = $Hash{ArrayInHash}; @Array2 = @$ArrayRef; print "\nArray from ordinary scalar reference: @Array2";


    perldoc perlref
Re: How to use hash of arrays
by tphyahoo (Vicar) on Feb 16, 2005 at 14:01 UTC
    Actually, contra my earlier post,
    use strict
    would have diagnosed the problem after all.
    #from http://www.perlmonks.org/?node_id=431329 #use strict; #Uncomment me to get to the root of the problem. use warnings; my %Hash = ( ArrayInHash => [1,2,3,4], ); print "Array in hash: $Hash{ArrayInHash}"; #Only get error if you use strict; print "WRONG Dereffed Array in hash: @$Hash{ArrayInHash}"; print "RIGHT Dereffed Array in hash with brackets: @{$Hash{ArrayInHash +}}";
    Use the strictures!
Re: How to use hash of arrays
by tphyahoo (Vicar) on Feb 16, 2005 at 09:09 UTC
    Don't forget to
    use strict; use warnings;
    Though that wouldn't have helped you here, it's a good habit.