2. List

A list in Perl is a collection of single values or scalars (comma separated and between parentheses).

A list can empty:

(); # an empty list is evaluated as false, all other lists are true
A list can have variables:
$name = "Reinier"
(1, 2, "xyz", $name);
... or an expression:
$name = "Reinier"
(4 + 7, "xyz", $name, 3 * 2); # The list contains the scalar values: 11 xyz Reinier 6
... or (built-in) functions:
$name = "Reinier"
(4 + 7, "xyz", $name, 3 * 2, sin(5)); # The list contains the scalar values: 11 xyz Reinier 6 -0.958924274663138

2.1 Create

2.1.1 The range operator
The range operator is a handy tool to create lists or variables.
(a .. z, 1 .. 10); # The list contains all the letters of the alphabet, followed by the numbers 1 till 10
(1.5 .. 3.5); # The list equals (1.5, 2.5, 3.5)
(7 .. 7); # The list contains only the scalar value 7
(8 .. 7); # The list is empty
(-1, 1..3, 10); # The list equals (-1, 1, 2, 3, 10)
($a .. $b) # The list equals ($a, $a+1, $a+2, ...) if $a is lower than $b
($x, $y, $z) = (1 .. 3) # $x = 1, $y = 2, $z = 3
($v, $w, $x, $y, $z) = ("01" .. "05"); # $v = 01, $w = 02, $x = 03, $y = 04, $z = 05
2.1.2 Can a list contain another list?
Yes, a list can contain another list. However, it makes little sense: a list contains scalar values, so the list will be flattened and contains only scalar values. This is called: list-flattening.
print(1, (2, 3), (4 .. 7)); # output: 1234567
2.1.3 Short hand: qw operator
The qw operator ('quote words') assigns a string to a list of single quoted elements (''). You can use different types of delimiters. For me are the bracketing characters (, <, [ or { as delimiter the best ones:
qw(a b c) is equivalent to ('a', 'b', 'c')
qw<a b c> is equivalent to ('a', 'b', 'c')
qw[a b c] is equivalent to ('a', 'b', 'c')
qw{a b c} is equivalent to ('a', 'b', 'c')
Notice that with qw, variable interpolation ( = replacing a variable with the value of that variable) is not possible.

Lists with only identical values can be made with the repetition operator 'x' (see 4.1.2 Repetition operator)
($first, $second, $third, $fourth) = ('on') x 4; # output: ('on', 'on', 'on', 'on') 

2.2 Access

2.2.1 Assigning scalar values from a list to a scalar variable
$first_char = (a .. z, 1 .. 10)[0]; # $first_char contains 'a'. A list is zero-based (1)
$third_char = (a .. z, 1 .. 10)[2]; # $third_char contains 'c'.
$first_second_third_fourth_char = (a .. z, 1 .. 10)[0..3]; # $first_second_third_fourth_char contains 'abcd'
$first_second_fourth_char = (a .. z, 1 .. 10)[0,1,3]; # $first_second_fourth_char contains 'abd'
The last two code examples give a so called slice of the list (a .. z, 1 .. 10). The last character(s) can be found with a negative index:
$last_char = (a .. z, 1 .. 10)[-1]; # $last_char contains '10'.
$last_four_char = (a .. z, 1 .. 10)[-1,-2,-3,-4]; # $last_four_char contains '10987'.
$last_four_char = reverse(a .. z, 1 .. 10)[0..4]); # $last_four_char contains '10987'.
2.2.2 Assigning scalar values from one list to another list
A list and its elements can be assigned to lists and variables
($a, $b) = (1, 2); # $a equals 1 and $b equals 2
($a, $b) = ($b, $a); # $a equals 2 and $b equals 1
($a, $b) = (1, 2, 3); # the value 3 will be not used
($a, $b, $c) = (1, 2); # $c is empty
The split function is a handy tool to assign string values to variables.
$str = "23 April 2023";
($day, $month, $year) = split (" ", $str); # the delimiter is a single space: " "
print($month); # $month equals April
2.2.3 List member
If you want to know if an item is a member of a list, use the function grep:
if (grep{ "Daniel" eq $_ } qw(Sebastian Daniel Florence)) {
print("True\n");
}
else {
print("False\n");
}
or more concise:
(grep{ "Daniel" eq $_ } qw(Bastian Daniel Florence)) ? (print("True\n")) : (print("False\n"));
or still more compact with a regex (Chapter 9):
(grep{ /Daniel/ } qw(Bastian Daniel Florence)) ? (print("True\n")) : (print("False\n"));

2.3 Print

The agrument of the function 'print' is a list! The following will be clear:
print("(4 + 7, 'xyz', $name, 3 * 2, sin(5))"); # prints a literal string, although $name is replaced by Reinier

print(4 + 7, 'xyz', $name, 3 * 2, sin(5)); # prints 11 xyz Reinier 6 -0.958924274663138
More on the subroutine 'print' in Chapter 3.

2.4 Iterate

The recommended way to iterate over a list is using 'foreach'. The following four code snippets give the same result: a, b, c on separate lines.
foreach $item ("a", "b", "c") {
print("$item\n");}
foreach $item (qw(a b c)) {
print("$item\n");
}
foreach (qw(a b c)) {
print($_ . "\n"); # all list elements are passed to the special variable
}
foreach (qw(a b c)) {
print; # 'print;' is a short cut for 'print($_);'
print("\n");
}
foreach (1 .. 5) {
print; # 12345
print("\n");
}
foreach (reverse(1 .. 5)) { # (5 .. 1) doesn't work!
print; # 54321
print("\n");
}

2.5 Operate

Perl built-in functions can have a list as argument. A few examples (with nested functions!).
print(reverse(1, (2, 3), (4 .. 7))); # output: 7654321
print(join(":", sort(qw/c b a/))); # output: a:b:c
The functions 'grep' and 'map' are used for filtering and transforming elements of a list. Each element is passed to the special variable and the expression will be evaluated.
print(join(" % ", grep{ $_ > 29 } (1, 50, 10, 30, 20))); # output: 50 %  30
print(join(" % ", map{ $_ - 2 } (1, 50, 10, 30, 20))); # output: -1 % 48 % 8 % 28 % 18
print(join(" % ", map{ $_ - 2 } sort(1, 50, 10, 30, 20))); # output: -1 % 8 % 18 % 28 % 48

2.6 List: how-to


1. How to count distinct elements?

use List::Util qw(uniq);
$count = scalar(uniq qw/1 2 2 3 3 3 4 4 4 4/);
print("$count\n"); # Output: 4
2. How to determine all unique elements that are contained in both lists (intersection)?
%intsect = ();
$intsect{$_} = 1 for (qw/1 2 2 2 3 3 3 3/, qw/ 5 4 4 4/);
@intersection = sort( keys(%intsect) );
print("@intersection\n"); # Output: 1 2 3 4 5
3. How to convert list to hash
%hash = ();
$id = 0;
foreach $value ('a'..'d') {
 $id++;
 $hash{"key_" . $id} = $value;
}

foreach $key ( sort( keys(%hash) ) ) {
 print("$key => $hash{$key}\n");
}		

Output:
key_1 => a
key_2 => b
key_3 => c
key_4 => d

4. how to convert hash to list
@list = ();
%hash = (
 key_1 => a,
 key_2 => b,
 key_3 => c,
 key_4 => d
);

@list = sort( values(%hash) );

print("@list\n"); # Output: a b c d

Exit: more on lists

Lists are input for arrays and built-in functions (you saw already a few examples). Studying Chapter 4 on arrays will give you insight into the endless possibilities of lists.

Footnotes
(1) A list is zero based. This means that the first element of the list has the index 0.