Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Allocation of anonymous arrays

by shmem (Canon)
on Feb 07, 2014 at 12:56 UTC ( #1073874=note: print w/ replies, xml ) Need Help??


in reply to Allocation of anonymous arrays

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'


Comment on Re: Allocation of anonymous arrays
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1073874]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2014-09-20 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls