Here are some more examples for you...
#!/usr/bin/perl use strict; use warnings; # simple 1D arrays: my @rowx = (1,2,3,4); my @rowy = qw (a b c); # A 2D array is an array of references to 1D arrays: # Note that Perl 2D arrays do not need to have the same # number of elements on each row! my @twoD = (\@rowx, \@rowy); foreach my $rowref (@twoD) { print "@$rowref\n"; } # prints # 1 2 3 4 # a b c # this can be a bit confusing... # In the first print, Perl knows that $twoD[0][1] means # an array element of @twoD, not be confused with # perhaps a simple scalar of the same name! # # In the second print, we get the value of $twoD, # a simple scalar my $twoD = "asdf"; print "$twoD[0][1]\n"; # prints 2 print "$twoD\n"; # prints asdf # In general, DO NOT use the same variable name for # two different sigils! Many things are "legal" in Perl # that you should not do!
Update: You will notice that in my foreach loop, I used the name "rowref" instead of just "row". Perl wouldn't care. But naming the iteration variable like this helps me to remind myself to dereference that thing. YMMV.

In reply to Re: Multi-Dimensional Arrays and Array References by Marshall
in thread Multi-Dimensional Arrays and Array References by Leudwinus

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.