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

Hi people,

Okay I think this a fairly basic concept but I promise you I have done my research prior to posting here but I'm still hopelessly lost!

I've looked at the following posts: How do I make a hash of arrays?, Making a Hash of Arrays, How do I refer to an element of a hash of arrays as an array?

I create my array with...

foreach $line (readline FNAME){ @data = split /\t/, $line; foreach $bit (@data){ $bit = removeWhiteSpace($bit); } $id_veritasHash{$count} = \@data; $count ++; }

...and I try to display the array with..

while ( ($key, @value) = each %id_veritasHash){ foreach $temp (@value){ print "$key, $temp\n"; } }

But this just gives me (well I think it is anyway) an array reference...

13, ARRAY(0x121654) 7, ARRAY(0x121654) 5, ARRAY(0x121654)

Please can someone help me here I don't understand whats wrong!

Thanks in advance


Replies are listed 'Best First'.
Re: Hash of arrays
by FamousLongAgo (Friar) on Oct 27, 2002 at 18:30 UTC
    You're almost there - you are correctly storing the array reference in the hash. Remember, however, that a reference in perl is just a scalar -- if you want to get at the thing it's a reference to, you'll need to dereference it first:
    while ( my ($key, $reference) = each %id_veritasHash){ foreach my $temp (@{$reference}){ print "$key, $temp\n"; } }
    I've put in some my declarations just for good coding style. The key bit is @{$reference}, which could also be written @$reference. This is where you dereference the scalar to get at its referent, the stored array.

    Reference syntax is tricky, and so it the concept, if you are just getting started. I suggest reading through perlreftut and going on from there. See if you can figure out the concept to your satisfaction, and then tackle the syntax.
Re: Hash of arrays
by grantm (Parson) on Oct 27, 2002 at 19:09 UTC

    I think the bit you're missing is that a reference is a scalar. So instead of this:

    while ( ($key, @value) = each %id_veritasHash){

    You need this:

    while ( ($key, $value) = each %id_veritasHash){

    Then to loop through the items in the array that the scalar $value references, instead of this:

    foreach $temp (@value){

    You need this:

    foreach $temp (@$value){

    The final bit of the puzzle is that each of your references points to the same array (the output from your code shows the same hex value in the stringified reference). To create a different array each time through the loop, instead of this:

    @data = split /\t/, $line;

    You need this:

    my @data = split /\t/, $line;

    There are other ways to create new arrays, which you can read about in perlreftut.

      Both of the replies to my posting were fantastic thanks guys! Up voting all round!


Re: Hash of arrays
by tadman (Prior) on Oct 27, 2002 at 20:16 UTC
    If you see something that looks like ARRAY(0x123456) then this means you have an ARRAY reference, and need to de-reference it. Here's some typical examples:
    my @array = @$array_ref; my %hash = %$hash_ref; my $scalar = $$scalar_ref;

      This is a very useful quick referenece guide, thanks!