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

Hi Guys , I am new to perl so need ur help. I have this test code can u guys tell me what is happening in this . I asume that in the push statement we r telling the code that the value of the code is going to be an array and to push the value fred into that . I am trying to print the array thereafter but can't succeed , can u help me ...
%link = (); push (@ { $link{"testing"} }, "fred") ; print $link{"testing"}; #prints ARRAY(0x15d505c)

Replies are listed 'Best First'.
Re: 2 dimensional Hash list
by davido (Cardinal) on Aug 28, 2004 at 04:07 UTC

    ARRAY(x15d505c) is telling you that you're printing an array reference. You really want to print the array, and to do that you dereference the array ref.

    print "@{$link{testing}}\n";

    This solution works because @{....} dereferences the array-ref held in $link{testing}. Oh, and I wrapped it in double-quotes to take advantage of the fact that by default, lists (or arrays) interpolated in double quotes result in each element being interpolated into the string with a space between each element. Visually, it works out nicely usually. You can alter that behavior by tinkering with the Perl special variable $".

    By the way, if all you're trying to do is find a quick-n-dirty way to reliably print out the contents of %link, you can use Data::Dumper.

    use Data::Dumper; # your code goes here... # Now we'll print the results: print Dumper \%link;

    Dave

Re: 2 dimensional Hash list
by kvale (Monsignor) on Aug 27, 2004 at 23:43 UTC
    You are creating what is known as a hash of arrays (HoA), rather than a two-dimensional hash (HoH). To print the array, I would write this:
    my %link; push @{ $link{testing} }, "fred"; push @{ $link{testing} }, "stan"; print join " ", @{ $link{testing} }, "\n";
    You print statement did not work because you needed to dereference the array reference $link{testing}.

    -Mark

Re: 2 dimensional Hash list
by bobf (Monsignor) on Aug 27, 2004 at 23:52 UTC
    Take a look at perldsc, perlreftut, and perlref. They have examples that will help you use these kind of structures.
    HTH