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.


In reply to Re: Push array into array of arrays by Marshall
in thread Push array into array of arrays by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.