The code accesses this using the array references as a key, and as the "key" arrays are all distinct these references must be unique. So no problem there.

No problem except that the keys of that hash aren't anonymous arrays. They are strings, keys of hashes are always strings. You cannot get get back at the anonymous array from its string representation, which is there only as a label. Instead of the hex number attached to it (which actually is the address of a C structure), it could also have the md5 checksum over the array members attached.

my %fred = ( [1, 2, 3] => [1, 1, 0], [3, 4, 5] => [0, 1, 0], [0, 2, 4] => [1, 2, 1], ); for my $k (keys %fred) { print "$k element 0: '",$k->[0],"'\n"; print "array: (",join(",",@$k),")\n"; } __END__ ARRAY(0x17ba658) element 0: '' array: () ARRAY(0x17b3b80) element 0: '' array: () ARRAY(0x1796998) element 0: '' array: ()

You might suspect, that the anonymous arrays went out of scope after the keys were generated out of them, so storing the arrays somewhere would keep them alive. That's true, but even so, the original arrays are not accessible via their string representation:

my @ary = ([1, 2, 3],[3, 4, 5],[0, 2, 4]); my %fred = ( $ary[0] => [1, 1, 0], $ary[1] => [0, 1, 0], $ary[2] => [1, 2, 1], ); for my $k (keys %fred) { print "$k element 0: '",$k->[0],"'\n"; print "array: (",join(",",@$k),")\n"; } __END__ ARRAY(0x19cca60) element 0: '' array: () ARRAY(0x19ccb80) element 0: '' array: () ARRAY(0x19af998) element 0: '' array: ()

If you want a reversible hash which allows you to get at the values of the arrays identified by their stringy names, you need to construct two hashes - one to map the strings to the arrays, and the hash with them strings as keys and values:

my @ary = ( [1, 2, 3], [1, 1, 0], [3, 4, 5], [0, 1, 0], [0, 2, 4], [1, 2, 1], ); my (@arystrings, %aryhash); for my $ary ( @ary ) { $aryhash{$ary} = $ary; push @arystrings, scalar $ary; # same string as the key above } my %fred = @arystrings; # treats @arystrings as (key,value,key,value,. +..) list print_stuff(); %fred = reverse %fred; # reverse hash print_stuff(); sub print_stuff { for my $k (keys %fred) { print "$k element 0: '",$aryhash{$k}->[0],"'\n"; print "array: (",join(",",@{$aryhash{$k}}),")\n"; } } __END__ ARRAY(0x25e6998) element 0: '1' array: (1,2,3) ARRAY(0x2603b80) element 0: '3' array: (3,4,5) ARRAY(0x260f948) element 0: '0' array: (0,2,4) ARRAY(0x260f8d0) element 0: '0' array: (0,1,0) ARRAY(0x260f9c0) element 0: '1' array: (1,2,1) ARRAY(0x2603a60) element 0: '1' array: (1,1,0)

Note that keys produces the keys of a hash in random order.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Allocation of anonymous arrays by shmem
in thread Allocation of anonymous arrays by OwlHoot

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.