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 | |
|
Re: Reference to a hash in an array of hashes
by liz (Monsignor) on Aug 13, 2003 at 18:46 UTC | |
|
Re: Reference to a hash in an array of hashes
by hardburn (Abbot) on Aug 13, 2003 at 18:58 UTC | |
|
Re: Reference to a hash in an array of hashes
by NetWallah (Canon) on Aug 13, 2003 at 19:39 UTC | |
|
Re: Reference to a hash in an array of hashes
by antirice (Priest) on Aug 13, 2003 at 19:32 UTC | |
|
Re: Reference to a hash in an array of hashes
by Brutha (Friar) on Aug 14, 2003 at 10:25 UTC | |
|
Re: Reference to a hash in an array of hashes
by vlad (Novice) on Aug 13, 2003 at 19:49 UTC |