Glad to help a new Perler starting on the journey.

Let me congratulate you on some stuff that you definitely did right! You thought about the problem yourself, you showed some code that you had written and what that code did, and asked a clear question. Basically the more work that you are doing, the more help that you are going to get.

I suspect that a next question is going to be "how do I print this?". So, see below...

I made a new blank array, @another_compass. Then to populate this, I showed some push operations instead of a fixed declaration statement. Just like @compass, @another_compass contains references to arrays. Essentially, what [ "NW", "N", "NE" ] means, is allocate some new memory (which has no name known to the program), an "anonymous array" and put 3 things in it, "NW", "N", "NE". Then the reference to that memory is pushed onto @another_compass.

To print this, each thing in @another_compass is a reference to an array. So, I iterate over each reference. @$compass_row says to expand this reference to an array back into the original array. Enclosing @$compass_row in quotes causes a space to be inserted between elements. Try it without the quotes.

Indicies can be used in Perl, but Perl has many iterators that help avoid having to do that. "off by one" errors are some of the most common boo-boos in programming. Using a Perl iterator avoids that problem by dispensing with the index all together. Of course there are times when our buddy, [ i ] is needed.

The Data::Dumper module is a fantastic tool for easily printing complex Perl structures. play with that too!

Have fun!

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @compass = ( ["NW", "N", "NE"], ["W", "center", "E"], ["SW", "S", "SE"], ); my @another_compass; push (@another_compass, ["NW", "N", "NE"]); push (@another_compass, ["W", "center", "E"]); #print Dumper \@another_compass; # uncomment to see what this does print "Dumping another_compass...\n"; foreach my $compass_row (@another_compass) { print "@$compass_row\n"; } __END__ Prints: Dumping another_compass... NW N NE W center E

In reply to Re^4: simple array question by Marshall
in thread simple array question by tw

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.