Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Push array into array of arrays

by johngg (Canon)
on Dec 18, 2018 at 15:29 UTC ( [id://1227404]=note: print w/replies, xml ) Need Help??


in reply to Push array into array of arrays

Having a bit of a guess here but could it be that you need to push the array rather than a reference to the array? The following might illustrate the difference. Note that after the first push @AoA has three elements, the last of which is a reference to @newArr, but there are five after I pop off the array reference and do the second push of the array rather than a reference to it, the last three being the same values as are in @newArr.

johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -MData::Dumper -E + ' my @AoA = ( [ 1, 2, 3 ], [ qw{ a b c } ], ); say Data::Dumper->Dumpxs( [ \ @AoA ], [ qw{ *AoA } ] ); my @newArr = qw{ x y z }; push @AoA, \ @newArr; say Data::Dumper->Dumpxs( [ \ @AoA ], [ qw{ *AoA } ] ); say $AoA[ -1 ]; say qq{@{ $AoA[ -1 ] }}; pop @AoA; push @AoA, @newArr; say Data::Dumper->Dumpxs( [ \ @AoA ], [ qw{ *AoA } ] );' @AoA = ( [ 1, 2, 3 ], [ 'a', 'b', 'c' ] ); @AoA = ( [ 1, 2, 3 ], [ 'a', 'b', 'c' ], [ 'x', 'y', 'z' ] ); ARRAY(0x555eb3457760) x y z @AoA = ( [ 1, 2, 3 ], [ 'a', 'b', 'c' ], 'x', 'y', 'z' );

I hope this is helpful.

Update: Expanded explanation.

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-29 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found