in reply to -> Is Optional ?
ree:
Just a bit of clarification on RichardK's answer: Notice how it says 'between' subscripts. So after the first one, it's always optional. Before the first one, though, you need to tell perl that you're accessing a value through a reference, and there are two ways you can do so. Either add a '$' to the front, or use a '->' after the scalar:
my @a = ( 1, 4, 9, 16); # squares my @b = ( 1, 8, 27, 64); # cubes my @c = ( \@a, \@b ); # two arrays my $d = \@a; my $e = \@c; # OK: @a is an array print $a[0]; # Wrong: $d is a reference, not an array! print $d[0]; # OK: Both of these are fine, though print $d->[0]; print $$d[0]; # with multiple subscripts: print $e->[0]->[0]; print $e->[0][0]; # same as above, since -> is optional between sub +scripts print $$e[0][0]; # also same as above
You may find yourself preferring an extra '$' at the front (which is what I usually use) or using the '->' (which I use when I want to alert myself to the fact that I'm using a reference). You need to be comfortable with both, since you'll frequently encounter both notations.
Update: D'oh! I bungled my variable declarations, as caught by roho and explained by Athanasius. Corrected (converted square brackets to parenthesis in first three lines).
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: -> Is Optional ?
by roho (Bishop) on Dec 29, 2012 at 16:15 UTC | |
by Athanasius (Archbishop) on Dec 29, 2012 at 16:34 UTC |