in reply to Re: simple array question
in thread simple array question

The reason that it didn't work, is that you didn't follow the directions from eyepopslikeamosquito!

There is a difference between: ( paren , [ square bracket and { curly brace!

By using "(" you would get an array of arrays. When you add the extra [, you are adding an additional dimension to the structure and getting arrays of arrays of arrays. So you have to throw in an additional de-referencing operator.

#!/usr/bin/perl use strict; use warnings; my @compass = ( ["NW", "N", "NE"], ["W", "center", "E"], ["SW", "S", "SE"] ); my @compassB = ( [ ["NW", "N", "NE"], ["W", "center", "E"], ["SW", "S", "SE"] ], [ ["A", "B", "C"], ["D", "E", "F"] ] ); print $compass[0]->[1]; #prints N print $compassB[0]->[0]->[1]; #prints N print $compassB[1]->[0]->[1]; #prints B my $ref=\@compassB; #added example with reference print $ref->[0]->[0]->[1]; #prints N my $firstAoA = $compassB[0]; print $firstAoA->[0][1]; #prints N
In a multi-dimensional structure everything is a reference until you get to the last thing which is the actual data.

Replies are listed 'Best First'.
Re^3: simple array question
by tw (Acolyte) on Jan 03, 2011 at 04:22 UTC

    thanks Marshall! the code explains the questions i had about the difference between ( and [

    sorry, i just hadn't read eyepopslikeamosquito very helpful reply before posting again...was still trying different things.

      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

        Thanks for the encouragement Marshall

        You have partly anticipated my next hurdle

        I am ok with using foreach to loop through all the elements but am struggling with returning particular values....for example if I only wanted to return the first value of @another_compass into a new array. i.e.

        NW W SW

        have tried a number of things similar to this...

        my @first_value = (); for my $compass_row (@another_compass) { push (@first_value, shift); }
        but obviously not correct, I used a split in place of shift with a slightly different problem so I thought shift would work. Although i assume you would need a different solution if the second value was required. Any pointers will be appreciated.