Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

push (@myArrofArray, \@myArray);

1) I tried to print @myArrofArray, but i get this "ARRAY(0x7c8f60)" which I couldn't get any relevant results on Google. :(

I tried to use perl debugger, x Dumper \@myArrofArray

but there is still one original array in it, without the new (@myArray)

May I know if the line is correct?

If it is correct, then I have trace back to check maybe the array isn't being imported into array in the first place, as it was retrieved sql results.

2) I am trying to get last array in the myArrofArray,

push(@a,pop(@myArrofArray));

print @a, I got ARRAY(0x858bd8)

expected output: @a = ('x', 'y', 'z', 8,9,7);

Can anyone point out the mistake?

I can provide more information if it is needed. Thanks!!

Replies are listed 'Best First'.
Re: Push array into array of arrays
by haj (Vicar) on Dec 18, 2018 at 14:31 UTC

    With push (@myArrofArray, \@myArray); you are adding a reference to @myArray to @myArrofArray. Printing Array references looks like ARRAY(0x7c8f60), but the number in parentheses will be different every time.

    If you want to unfold the individual elements of @myArray into @myArrofArray, use:

    push (@myArrofArray, @myArray);

    If you want to print individual elements of @a, unfold the elements before pushing them:

    push(@a,@{pop(@myArrofArray)});
Re: Push array into array of arrays
by kcott (Archbishop) on Dec 18, 2018 at 14:50 UTC

    It sounds like you're doing something like:

    $ perl -E 'my @x = ([qw{a b c}]); say "@x"' ARRAY(0x7fab41804248)

    But you really wanted to do something more like:

    $ perl -E 'my @x = ([qw{a b c}]); say "@{$x[0]}"' a b c

    Please take a look at SSCCE. If you provide us with something we can run to reproduce your results, we'll be able to provide better help.

    Also, just showing expected output — without showing the input and how it was processed — serves no useful purpose. Perhaps you accidentally omitted some important information; however, I see nothing in your post that indicates why "('x', 'y', 'z', 8,9,7)" would be expected.

    — Ken

Re: Push array into array of arrays
by bliako (Abbot) on Dec 18, 2018 at 15:52 UTC

    For your first question,

    I tried to use perl debugger, x Dumper \@myArrofArray

    You must be doing something wrong, Dumper will print all the arrays inside your ArrofArray, one after the other.

    btw, if it's the name of the array (@myArray) you are expecting to see, you will see none, only its contents.

    use Data::Dumper; my @myArray = (1,2,3); my @myArrofArray = (); push (@myArrofArray, \@myArray); print Dumper(\@myArrofArray);
    $VAR1 = [ [ 1, 2, 3 ] ];
Re: Push array into array of arrays
by johngg (Canon) on Dec 18, 2018 at 15:29 UTC

    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

Re: Push array into array of arrays
by Marshall (Canon) on Dec 18, 2018 at 21:26 UTC
    push(@a,pop(@myArrofArray));
    print @a, I got ARRAY(0x858bd8)

    expected output: @a = ('x', 'y', 'z', 8,9,7);
    Can anyone point out the mistake?

    A Perl 2D array is array of references to arrays. Above, you pop an array ref off of @myArrofArray and then push that array ref onto @a. So @a will contain a single element, a single scalar array reference.

    Consider:

    my $array_ref = pop @myArrofArray; print "@$array_ref\n"; #dereference the reference push @a, @$array_ref; print "@a\n"; #now @a contains the entire row of values
    To print the values of a "row" in a 2D array, you have to "dereference" the reference.

    PS: the "@a" and "@b" have special meanings within Perl, it is best to use x,y or something else.

    Update: I got a few comments about not using @a or @b. My advice was an oversimplification. $a and $b have special meanings within Perl. It is completely allowed to use @a or %a - these do not have a special meaning. However I avoid those variables and recommend that others do the same. Leave the vars names a and b to Perl. This is to avoid any confusion. I guess you can have $a[0] which is first element of @a and that might be confused with $a which is completely different.

      PS: the "@a" and "@b" have special meanings within Perl

      That's the first I've heard of this. What are the special meanings of @a and @b?

      I don't think there are special meanings for "@a" and "@b", I think you may be thinking about "$a" and "$b" which are identified as special variable for use in perl sort, see perlvar. perlcritic will create a message "Magic variable "@a" should be assigned as "local" at line..."; however, I am not aware of and cannot find any reference to "@a" or "@b" as being a magic variable

      Please explain if I am missing something!

      Cheers, lbe

        Hi Ibe,
        You have this completely right!

        My advice was over simplified.
        In Perl, the various sigils (example: %,$,@) have their own namespaces.

        Yes, it is possible to have @x and $x or even %x to be distinct things.
        $a is different than @a.

        I do not believe that using the same alphanumeric name for a hash, array or scalar is a good idea.
        Not everything that is allowed by Perl is "good idea".

        I recommend and advise avoiding using "a" or "b" for any kind of user variable.
        Consider $a[1] -- that accesses @a instead of the $a scalar. This can be confusing.

        I stand by my recommendation to avoid any user variable named "a" or "b" or using the same string to define things like: %xyz, $xyz, @xyz.
        Use different names for these very different things.

Re: Push array into array of arrays
by Anonymous Monk on Dec 20, 2018 at 02:15 UTC
    Thank you guys for the helpful replies !
    #!/usr/bin/perl5.14.1 use DBI; use Data::Dumper; $dbh = DBI->connect("DBI:host", 'name', 'pw', { RaiseError => 1, AutoCommit => 1 }, ); @tesxt = ('perl','python','pearl', 'pear', 'pink', 'pong'); @array1 = ('fan','friend','fang', 'fun', 'food', 'flow'); @array2 = (21,22,23,14, 15, 16); @array3 = ('oh','orange','okay','ostrich', 'open', 'osaka'); @jumpjump= ($tesxt[0], $tesxt[2], $tesxt[3], 'kit','kat',7); push (@data, \@tesxt); push (@data, \@jumpjump); push (@data, \@array1); push (@data, \@array2); push (@data, \@array3); splice (@jumpjump); @jumpjump= ($tesxt[0], $tesxt[2], $tesxt[3], 'koo','kaa',7); push (@data, \@jumpjump); print Dumper \@data; $sth = $dbh->prepare("insert into table_test (just, random, field, for +, testing, purposes) values (?, ?, ?, ?, ?, ?)"); $dbh->begin_work(); foreach my $row (@data) { $sth->execute(@$row); } $dbh->commit(); $dbh->disconnect;
    Results looked like this
    $VAR1 = [ [ 'perl', 'python', 'pearl', 'pear', 'pink', 'pong' ], [ 'perl', 'pearl', 'pear', 'koo', 'kaa', 7 ], [ 'fan', 'friend', 'fang', 'fun', 'food', 'flow' ], [ 21, 22, 23, 14, 15, 16 ], [ 'oh', 'orange', 'okay', 'ostrich', 'open', 'osaka' ], $VAR1->[1] ];
    I have no idea where the last $VAR1->1 came from , and I noticed how the @jumpjump being replaced, since I have array with the same name, tho I splice it before putting new values in it, still it's being replace in the big @data array.

    Anyone has any idea?

    I wish to get both the different @jumpjump in the db.

    ***I apologized for not putting using strict here, it's a quick and dirty code that get me started, it compiled, i promise. :)

      Added strict and warnings
      #!/usr/bin/perl5.14.1 use DBI; use Data::Dumper; my @data; my $dbh = DBI->connect("DBI:host", 'name', 'pw', { RaiseError => 1, AutoCommit => 1 }, ); my @tesxt = ('perl','python','pearl', 'pear', 'pink', 'pong'); my @array1 = ('fan','friend','fang', 'fun', 'food', 'flow'); my @array2 = (21,22,23,14, 15, 16); my @array3 = ('oh','orange','okay','ostrich', 'open', 'osaka'); my @jumpjump= ($tesxt[0], $tesxt[2], $tesxt[3], 'kit','kat',7); push (@data, \@tesxt); push (@data, \@jumpjump); push (@data, \@array1); push (@data, \@array2); push (@data, \@array3); splice (@jumpjump); @jumpjump= ($tesxt[0], $tesxt[2], $tesxt[3], 'koo','kaa',7); push (@data, \@jumpjump); print Dumper \@data; my $sth = $dbh->prepare("insert into table_test (just, random, field, +for +, testing, purposes) values (?, ?, ?, ?, ?, ?)"); $dbh->begin_work(); foreach my $row (@data) { $sth->execute(@$row); } $dbh->commit(); $dbh->disconnect;
        $VAR1->1 is the array of arrays. but since it has different values, how can i ensure it to be inserted, without overriding the previous ?