The [] brackets are used to create a list reference. A reference is a scalar ($ sigil), not an array (@ sigil). To understand references, have a look at perlref. You could get it to work by dereferencing the $compass reference, like this:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $compass =
[
["NW", "N", "NE"],
["W", "center", "E"],
["SW", "S", "SE"]
];
print Dumper($compass);
print $compass->[0][1];
| [reply] [d/l] [select] |
| [reply] |
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. | [reply] [d/l] |
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.
| [reply] |
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
| [reply] [d/l] |