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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |