Hi perltux,

Now if I want to reference ... To access the value

I think the terminology you're using is confusing the issue a bit. Could you explain with code what you are trying to do? It would also be very useful if you could show your data structure via Data::Dumper or Data::Dump.

When you say $AN{data}[0x16], %AN is of course a hash, in which the value $AN{data} is a reference to an array, and you are then indexing that array with the subscript [0x16] (which, as tybalt89 already pointed out, is actually the 23rd element of the array at index 22). The only reference at this point is the one stored at $AN{data}, which is the reference to the array. Note that $AN{data}[0x16] is the same thing as $AN{data}->[0x16], where -> is the arrow (dereference) operator, which is implied and optional between bracketed subscripts.

You access the value stored at $AN{data}[0x16] with, for example, my $x = $AN{data}[0x16]; and set the value with $AN{data}[0x16] = $x;. That much should be obvious, but that value may also be a reference:

If you want $AN{data}[0x16] to store a reference to something else, then for example you need to say something like my $val = 123; $AN{data}[0x16] = \$val;. Then $AN{data}[0x16] contains a reference to the scalar $val and you dereference that reference with ${ $AN{data}[0x16] }, so ${ $AN{data}[0x16] } = 456; will actually set $val to 456. Another example is my @foo = qw/a b c/; $AN{data}[0x17] = \@foo;, in which case you can access that array using the implicit dereferencing mentioned above, so $AN{data}[0x17][2] accesses the value that is currently "c".

Note that the syntax $$AN{data}[0x16] means something else: it dereferences a hash reference named $AN - think of it as ${$AN} {data}[0x16] or $AN->{data}->[0x16].

On the other hand, if you want to take a reference to $AN{data}[0x16], you can say my $r = \$AN{data}[0x16];, now $r holds a reference to that element of the data structure. You can access that element of the data structure (get and set the value of $AN{data}[0x16]) with $$r.

I recommend you read perlreftut, perlref, and perldsc - that gives you just about everything you need to know about references in Perl.

Hope this helps,
-- Hauke D

Made few updates to wording and a few additions


In reply to Re: complex data structures by haukex
in thread complex data structures by perltux

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.