in reply to Re: Allocation of anonymous arrays
in thread Allocation of anonymous arrays

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.

Replies are listed 'Best First'.
Re^3: Allocation of anonymous arrays
by LanX (Saint) on Feb 07, 2014 at 16:11 UTC
    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]], ]
        I agree about encapsulation, thats why I mentioned Tie::Hash after demonstrating the raw mechanics in a REPL.

        If tied or functional or OO or whatever interface... YMMV! :)

        Anyway we are again speculating about the OPs real intention, he just showed us enough to think it's pointless for most thinkable motivations.

        So pointless that Perl could/should warn about using literal arrays or hashes as keys...

        update

        tl;dr ...

        I just used a reverse in my example to avoid triviality with a minimal amount of code, it was a workaround not a goal.

        The general problem is to use complex data-structures as keys, see also Using hashes for set operations... for a similar discussion.

        Cheers Rolf

        ( addicted to the Perl Programming Language)