Course of Raku / Advanced / Containers / Ordered containers
Lists
A list is also an ordered collection of values, but, unlike an array, it is immutable: you cannot change, add, or remove its elements. A list is written as a comma-separated sequence of values, often placed inside parentheses:
my $colours = ('red', 'green', 'blue');You can read the elements of a list by their index, and ask how many there are, just as with an array:
my $colours = ('red', 'green', 'blue');
say $colours[0]; # red
say $colours.elems; # 3The difference shows up when you try to change an element. With an array it works, because each element is a container:
my @array = 1, 2, 3;
@array[0] = 10;
say @array; # [10 2 3]With a list it is an error, as the elements of a list are plain values, not containers:
my $list = (1, 2, 3);
$list[0] = 10;The second program stops with a message:
Cannot modify an immutable List ((1 2 3))
in block <unit> at t.raku line 2So, use an array (the @ sigil) when you need to change
the contents, and a list when you only need to keep a fixed sequence of
values.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Arrays | Quiz — Arrays and lists →
💪 Or jump directly to the exercises in this
section.