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

Given this code:
# a hash holding a reference to the array my %hash; print '$aref is ' . $aref ."\n"; $hash{'aref'} = $aref; print '$hash{\'aref\'} is ' . $hash{'aref'}. "\n"; # get h_ version of array reference my $h_aref = $hash{'aref'}; print 'array length is ' . scalar(@$h_aref) . "\n"; push @$h_aref, 'value 2'; print 'after 2nd push array length is ' . scalar(@$h_aref) . "\n"; print 'after 2nd push [0] array value is [' . $$h_aref[0] . "]\n"; print 'after push [1] array value is [' . $$h_aref[1] . "]\n"; # now try to access without intermediate $h_aref #print 'array length is ' . scalar(@$hash{'aref'}) . "\n"; # error #print 'array length is ' . scalar(@$$hash{'aref'}) . "\n"; # error
How do I access the array without the intermediate $h_aref?

---------------------------

It is indeed a curly braces thing
my %hash; print '$aref is ' . $aref ."\n"; $hash{'aref'} = $aref; print '$hash{\'aref\'} is ' . $hash{'aref'}. "\n"; # get h_ version of array reference my $h_aref = $hash{'aref'}; print 'array length is ' . scalar(@$h_aref) . "\n"; push @$h_aref, 'value 2'; print 'after 2nd push array length is ' . scalar(@$h_aref) . "\n"; print 'after 2nd push [0] array value is [' . $$h_aref[0] . "]\n"; print 'after 2nd push [1] array value is [' . $$h_aref[1] . "]\n"; # now try to access without intermediate $h_aref #print 'array length is ' . scalar(@$hash{'aref'}) . "\n"; # error #print 'array length is ' . scalar(@$$hash{'aref'}) . "\n"; # error print "now without the intermediate \$h_ref\n"; print 'array length is ' . scalar(@{$hash{'aref'}}) . "\n"; push @{$hash{'aref'}}, 'value 3'; print 'array length after 3rd push is ' . scalar(@{$hash{'aref'}}) . " +\n"; print 'after 3rd push [0] array value is [' . $hash{'aref'}[0] . "]\n" +; print 'after 3rd push [1] array value is [' . $hash{'aref'}[1] . "]\n" +; print 'after 3rd push [2] array value is [' . $hash{'aref'}[2] . "]\n" +;

Thanks to Data::Dumper and References quick reference

Replies are listed 'Best First'.
Re: accessing array through array reference in hash
by ptum (Priest) on Dec 29, 2005 at 00:13 UTC

    I think you need curly braces around $hash{'aref'} to explicitly cast it as an array. (Not sure if my terminology is correct.)

    #!/usr/local/bin/perl -w use strict; my @array = ('one','two','three'); my $aref = \@array; my %hash = (); $hash{'aref'} = $aref; print "Array length is:", scalar(@{$hash{'aref'}}), " \n";
    Array length is: 3

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      Yep, one could try to memorize that @$ dereferences act on the first part of the expression, namely the '$hash' part. If you want to dereference more than the first part, you enclose it in curlies. perldoc perlref knows more about this.

      Ordinary morality is for ordinary people. -- Aleister Crowley
Re: accessing array through array reference in hash
by TedPride (Priest) on Dec 29, 2005 at 03:05 UTC
    print "Array length is: ", $#{$hash{'aref'}}+1, "\n"; print "$_\n" for @{$hash{'aref'}};
    Or:
    print "@{$hash{'aref'}}";