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

Hello colleagues,

I am in the process of "enhancing" someone else's code. There is an array of hashes and I need to be able to reference an array element in it - the hash. I tried something like that:

#!/usr/bin/perl my @array; $array[0]{'key'} = "testline"; $item = \$array[0]; $array[0]{'key2'} = "testline2";

However, the $item can not be properly dereferenced for some reason. Here is the debugger session:

[me@system directory]$ perl -d aht.pl Loading DB routines from perl5db.pl version 1.19 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(aht.pl:2): my @array; DB<1> b 5 DB<2> c main::(aht.pl:5): $array[0]{'key2'} = "testline2"; DB<2> p $array[0]{'key'} testline DB<3> p $array[0] HASH(0x8086ab4) DB<4> p $$item HASH(0x8086ab4) DB<5> p $$item{'key'} Not a HASH reference at (eval 10)[/usr/lib/perl5/5.8.0/perl5db.pl:17] +line 2. DB<6> q [me@system directory]$

As one can see, $item is a proper reference, but it needs a cast of some sort. My experiments were unsuccessful - anyone have a hint?

Sincerely,
Vlad.

Replies are listed 'Best First'.
Re: Reference to a hash in an array of hashes
by benn (Vicar) on Aug 13, 2003 at 18:46 UTC
    $array[0] is already a reference to the hash. By doing $item = \$array[0], you're creating a reference to a reference. Try something like this...
    $array[0]{'key'} = "testline"; $item = $array[0]; print $item->{key}; #will print "testline"
    or if you prefer...
    %item = %{$array[0]}; print $item{key};
    Cheers,
    Ben.
Re: Reference to a hash in an array of hashes
by liz (Monsignor) on Aug 13, 2003 at 18:46 UTC
    $item = \$array[0];

    Why are you taking a reference here?

    $item = $array[0];
    $item is already a hash ref, as the following will show you:
    print "$_: $item->{$_}\n" foreach keys %{$item};

    Liz

Re: Reference to a hash in an array of hashes
by hardburn (Abbot) on Aug 13, 2003 at 18:58 UTC

    An easier way of viewing the internal structure of data is to use Data::Dumper:

    use Data::Dumper; print Data::Dumper->Dumper(@array);

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Reference to a hash in an array of hashes
by NetWallah (Canon) on Aug 13, 2003 at 19:39 UTC
    Following-up on benn's method of:
    %item = %{$array[0]}; # Creates a copy of the hash print $item{key};
    You could do that cheaper using TypeGlobs (Just showing off recently-acquired knowledge).
    no strict "vars"; # Allow local decs local *item = \%{$array[0]}; # Creates a ref. No copy. print $item{key};
    Details in perldoc perldata, under "Typeglobs and Filehandles".
Re: Reference to a hash in an array of hashes
by antirice (Priest) on Aug 13, 2003 at 19:32 UTC

    Check out perldsc for more information regarding data structures within perl. Also, use Data::Dumper to view what a structure contains.

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Reference to a hash in an array of hashes
by Brutha (Friar) on Aug 14, 2003 at 10:25 UTC
    Why did nobody tell Vlad that he can use the 'x' command instead of 'p' within the debugger to view structures?

    And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
    (Terry Pratchett, Small Gods)

Re: Reference to a hash in an array of hashes
by vlad (Novice) on Aug 13, 2003 at 19:49 UTC
    Thanks, everyone!!!!