Lists and Arrays
A list is an ordered sequence of scalars, written in parentheses: (1, 2, 3). An array is a variable that holds a list. Array variables start with @.
use strict;
use warnings;
my @planets = ("Mercury", "Venus", "Earth", "Mars");
my @primes = (2, 3, 5, 7, 11);
my @mixed = ("text", 42, 3.14);
my @empty = ();
my @words = qw(red green blue); # quote-words shortcut
print "Planets: @planets\n";
print "Primes: @primes\n";
print "Words: @words\n";
print "Count of planets: ", scalar(@planets), "\n";Planets: Mercury Venus Earth Mars
Primes: 2 3 5 7 11
Words: red green blue
Count of planets: 4
scalar(@array) gives the number of elements. You will see in the context lesson why my $n = @planets; gives the count too.
Accessing elements
Elements are numbered from 0. A single element is a scalar, so you use $ with square brackets. Negative indexes count from the end:
use strict;
use warnings;
my @queue = qw(ana ben chloe dev);
print "First: $queue[0]\n";
print "Second: $queue[1]\n";
print "Last: $queue[-1]\n";
print "Last index: $#queue\n";
$queue[1] = "bianca"; # replace an element
print "Second now: $queue[1]\n";
$queue[6] = "gus"; # extends the array; 4 and 5 become undef
print "Now ", scalar(@queue), " slots\n";
print "Slot 5 is ", (defined $queue[5] ? "set" : "undef"), "\n";First: ana
Second: ben
Last: dev
Last index: 3
Second now: bianca
Now 7 slots
Slot 5 is undef
$#queue is the last index (one less than the length). Assigning beyond the end grows the array and fills the gap with undef.
Slices
A slice picks several elements at once. Because the result is a list, a slice uses the @ sigil:
use strict;
use warnings;
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @q1 = @months[0 .. 2];
my @summer = @months[5, 6, 7];
my ($first, $second) = @months; # list assignment takes the first two
print "Q1: @q1\n";
print "Summer: @summer\n";
print "Start: $first, $second\n";
print "Last two: @months[-2, -1]\n";Q1: Jan Feb Mar
Summer: Jun Jul Aug
Start: Jan, Feb
Last two: Nov Dec
Lists flatten
When you put arrays inside a list, they are flattened into one long list. Perl lists never nest by themselves (nested structures need references, covered later):
use strict;
use warnings;
my @fruits = qw(apple pear);
my @veg = qw(carrot leek);
my @basket = (@fruits, "bread", @veg);
print "Basket: @basket\n";
print "Items: ", scalar(@basket), "\n";
my @copy = @basket; # copies all elements
$copy[0] = "mango";
print "Original first: $basket[0], copy first: $copy[0]\n";Basket: apple pear bread carrot leek
Items: 5
Original first: apple, copy first: mango
Assigning one array to another makes an independent copy.
Looping over an array
The foreach loop is the usual way to visit each element. Loop over indexes when you need the position:
use strict;
use warnings;
my @tasks = ("write tests", "fix bug", "deploy");
foreach my $task (@tasks) {
print "TODO: $task\n";
}
for my $i (0 .. $#tasks) {
print $i + 1, ". $tasks[$i]\n";
}TODO: write tests
TODO: fix bug
TODO: deploy
1. write tests
2. fix bug
3. deploy
Test yourself
Given my @a = (10, 20, 30);, what is $#a?
- 3
- 2
- 30
- -1
Which expression is a slice of the first three elements of @list?
$list[0..2]@list[0 .. 2]@list(0, 2)%list[0..2]
What does my @all = (@x, @y); produce when @x = (1, 2) and @y = (3)?
- A two-element array of arrays
(1, 2, 3)- An error
((1, 2), 3)