Arrays
Declaring a list
An array holds an ordered list of scalars under a name with the at sign. The parentheses on the right are a list, not a grouping, so commas separate its elements.
my @langs = ("perl", "ruby", "python"); my @sizes = (10, 20, 30); print "@langs\n"; # perl ruby python
perl demo.pl
perl ruby python
The same name may hold an array and a scalar at once, because the sigil is part of the name. @langs and $langs are separate storage.
Index and sigil change
To read one element you use the dollar sigil, because a single element is a scalar. The brackets carry the index, counting from zero.
my @langs = ("perl", "ruby", "python"); print $langs[0], "\n"; # perl print $langs[2], "\n"; # python print $langs[-1], "\n"; # python, last element
perl demo.pl
perl
python
python
Negative indexes count back from the end, so the last element is always -1. That works for arrays and for slices, and it removes most of the off-by-one arithmetic from list code.
Slices
A slice takes several elements at once and keeps their order. The sigil becomes an at sign because the result is a list, and the brackets hold the indexes.
my @langs = ("perl", "ruby", "python"); my @first_two = @langs[0, 1]; print "@first_two\n"; # perl ruby my @last_two = @langs[-2, -1]; print "@last_two\n"; # ruby python
perl demo.pl
perl ruby
ruby python
The last index
The special form $#array gives the index of the last element, which is one less than the number of elements. Use scalar @array when you want the count itself.
my @langs = ("perl", "ruby", "python"); print $#langs, "\n"; # 2 print scalar @langs, "\n"; # 3
perl demo.pl
2
3
Assigning to $#array truncates or extends the array, so it is a quick way to keep the first ten elements: $#list = 9;
Ranges and qw
The range operator builds a list from two endpoints. Combined with qw, which splits a string on whitespace, it covers most literal lists in real code.
my @digits = (1 .. 5); my @letters = ("a" .. "e"); my @flags = qw(-v --quiet --dry-run); print "@digits\n"; # 1 2 3 4 5 print "@letters\n"; # a b c d e
perl demo.pl
1 2 3 4 5
a b c d e
Ranges are lazy about nothing: 1 .. 1_000_000 really builds a million elements. For big sequences use a counting loop from lesson 3.7 instead.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which sigil does an array carry, and why does a single element switch to another one?
Answer
@, because an array is a list; a single element is a scalar, so it takes$The sigil names the kind of value you take from the name: the whole list with
@, one element with$. -
What does
$langs[-1]give for a three-element array?Answer
The last element
A negative index counts back from the end, so -1 is always the last element.
-
What is the difference between
$#langsandscalar @langsfor the same three elements?Answer
$#langsgives 2 andscalar @langsgives 3$#is the last index, one less than the count thatscalar @langsreturns. -
What does
$#list = 9;do to a longer array?Answer
It truncates the array to ten elements
Assigning to the last index sets the new end of the array, which drops everything after it or fills the gaps with undef.
-
What does
qw(-v --quiet --dry-run)build?Answer
A list of three strings, one per word
qwsplits the text on whitespace and returns the words as a list, quoting each of them for you. -
Which expression builds the list of the numbers 1 to 5?
Answer
(1 .. 5)The range operator takes the endpoints and steps by one;
qw(1 5)is the two words 1 and 5. -
What does this program print?
my @a = (1 .. 4); print scalar @a, " ", $#a, "\n";Answer
4 3
Four elements means the count is 4 and the last index is 3, because indexing starts at zero.
-
What does this program print?
my @langs = ("perl", "ruby", "python"); print "@langs[-2, -1]\n";Answer
ruby python
A slice keeps the order of the indexes, and -2 and -1 are the last two elements.
-
What does this program print?
my @a = (1 .. 3); print $a[-1] + $a[0], "\n";Answer
4
The last element is 3 and the first is 1, and the plus adds them.
-
What does this program print?
my @langs = ("perl", "ruby", "python"); print @langs[0, 1], "\n";Answer
perlruby
printgets a list, not one string, so nothing puts a separator between the elements;joinor an interpolated string would.
Article author: Arthur Isaev