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

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]], ]

Replies are listed 'Best First'.
Re^5: Allocation of anonymous arrays
by LanX (Saint) on Feb 08, 2014 at 16:19 UTC
    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)