Lists are similar to arrays but are immutable. It is not possible to change the size or the elements of a list. Use parentheses to create a list:
my $odd = (1, 3, 5, 7, 9);
To access individual elements of a list, use square brackets with the index:
say $odd; # 7
Although, it is not possible to set a new value:
$odd = 8; # Not OK
Such an attempt causes an exception:
Cannot modify an immutable List ((1 3 5 7 9)) in block <unit> at t.raku line 4
Assigning a list to an array
What happens when you save a list in a variable with the
my @data = (40, 42, 44);
In this case, the
@data variable contains an array, and you can modify its elements.
Variable vs list
Note that the fact that lists are immutable does not mean you cannot reuse the variable that keeps it.
my $elements = (1, 2); say $elements; # 1 $elements = (3, 4); say $elements; # 3
Complete the quiz that covers the contents of this topic.
💪 Or jump directly to the exercises to this section.