in reply to Allocation of anonymous arrays

References and their stringifications are unique as long as their data structure have not been released before.

But the deeper problem like others already mentioned, must be stressed out:

Perl has no way to get a reference from it's stringification!

It's a one way street...

Then reversing this hash is like treating a marijuana addict with cocaine.

This whole technique is pointless as long as you don't manually store a lookup-hash to be able to transform key-string to ref.

DB<107> $aref=[1,2,3]; $lookup{$aref}=$aref => [1, 2, 3] DB<108> \%lookup => { "ARRAY(0x8ffd450)" => [1, 2, 3] }

Tell your colleague there is no way to use literal arrays here cause the information gets lost.¹

Cheers Rolf

( addicted to the Perl Programming Language)

PS: as a side note, Python allows other data-types to be keys, but only if they are immutable ... like literal strings are.

update

¹) As long as he doesn't use a tied hash from a fancy CPAN module

Replies are listed 'Best First'.
Re^2: Allocation of anonymous arrays
by AnomalousMonk (Archbishop) on Feb 07, 2014 at 15:44 UTC
    This whole technique is pointless as long as you don't manually store a lookup-hash to be able to transform key-string to ref.

    But then, in the example given, you have to have the value of  $aref (i.e., the reference) in order to stringize it and use it look up the value of  $aref — which also seems pointless.

      There different shades of pointless in this discussion. :)

      you don't have to keep all $arefs after construction.

      DB<112> for $aref ( [1,2,3],[4,5,6],[7,8,9] ) { $lookup{$aref} = $aref; $hash{$aref} = [ reverse @$aref ]; } => "" DB<113> \%hash => { "ARRAY(0xa547e40)" => [9, 8, 7], "ARRAY(0xa5c2e68)" => [6, 5, 4], "ARRAY(0xa5c3188)" => [3, 2, 1], } DB<114> \%lookup => { "ARRAY(0xa547e40)" => [7, 8, 9], "ARRAY(0xa5c2e68)" => [4, 5, 6], "ARRAY(0xa5c3188)" => [1, 2, 3], } DB<115> print "@{$lookup{$_}}\n" for keys %hash 7 8 9 4 5 6 1 2 3

      Hiding all of this behind a tied hash should be feasible, (depending on implementation details of Tie::Hash , IAW when, where and how "stringization" happens )

      Anyway I didn't try to find such implementations on CPAN.

      One use case could be to implement sets of complex data structures including set operations

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      updates
      corrected link to Tie::Hash

        I would be more comfortable with a more familiar data structure organization. In addition to familiarity, I think it offers benefits when data elements are added/removed/mutated: as long as data element generation is encapsulated, there are no worries about construction and especially destruction.

        >perl -wMstrict -MData::Dump -le "my @data = map data_element($_), [1, 2, 3,], [4, 5, 6,], [7, 8, 9,], ; ;; sub data_element { return { forward => $_[0], backward => [ reverse @{ $_[0] } ] }; } ;; dd \@data; $data[2]{forward}[1] == $data[2]{backward}[1] or die 'Oops...'; ;; ;; use constant { FWD => 0, REV => 1, }; ;; sub data_element2 { return [ @{[]}[FWD, REV] = ($_[0], [ reverse @{$_[0]}]) ]; } ;; my @data2 = map data_element2($_), [ 8, 7, 6, ], [ 5, 4, 3, ], [ 2, 1, 0, ], ; dd \@data2; $data2[0][FWD][1] == $data2[0][REV][1] or die 'Oops2...'; " [ { backward => [3, 2, 1], forward => [1, 2, 3] }, { backward => [6, 5, 4], forward => [4, 5, 6] }, { backward => [9, 8, 7], forward => [7, 8, 9] }, ] [ [[8, 7, 6], [6, 7, 8]], [[5, 4, 3], [3, 4, 5]], [[2, 1, 0], [0, 1, 2]], ]