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.


In reply to Reference to a hash in an array of hashes by vlad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.