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
# Wrong: $d is a reference, not an array!
# OK: Both of these are fine, though
# with multiple subscripts:
print $e->; # same as above, since -> is optional between sub
print $$e; # 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).
When your only tool is a hammer, all problems look like your thumb.
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||
Others browsing the Monastery:
(6)As of 2017-01-24 22:43 GMT