You understand what an array is. If you push an array onto another array, you just wind up with a single bigger single dimensional array. A multi dimensional array is an array of references to array. Push a reference to the @e array onto the @f array. push @f,\@e; I suppose you could write, push @f,[@e], but that would expand @e into an anonymous array and then make a reference to that newly created anon array.

BTW, there can be huge problems if you don't use "my" variables. "my @e" creates a brand new @e array every time this is seen. The push, pushes a reference to the "current @e array". When the loop comes around again, a whole new different @e array is created.

use strict; use warnings; use Data::Dump qw(pp); my @f; for my $i (0..40) { my @e=($i+=2, $i+1); #same as ($i+2, $i+3) push(@f,\@e); } pp \@f; __END__ Not sure if this is what you want, but this code results in: [ [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [19, 20], [20, 21], [21, 22], [22, 23], [23, 24], [24, 25], [25, 26], [26, 27], [27, 28], [28, 29], [29, 30], [30, 31], [31, 32], [32, 33], [33, 34], [34, 35], [35, 36], [36, 37], [37, 38], [38, 39], [39, 40], [40, 41], [41, 42], [42, 43], ]
Update: Now that I think about it, $i+=2 is an extremely bad idea. $i is the loop variable. You are asking for big trouble if you attempt to modify the loop variable while you are within the for loop. It does work here, but in general, I would avoid it and go with my ($i+2, $i+3) formulation.

In reply to Re: How Perl can push array into array and then how retrieve by Marshall
in thread How Perl can push array into array and then how retrieve 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.