A list in Perl is a collection of single values or scalars (comma separated and between parentheses).
A list can empty:
A list can have variables:(); # an empty list is evaluated as false, all other lists are true
... or an expression:$name = "Reinier"
(1, 2, "xyz", $name);
... or (built-in) functions:$name = "Reinier"
(4 + 7, "xyz", $name, 3 * 2);# The list contains the scalar values: 11 xyz Reinier 6
$name = "Reinier"
(4 + 7, "xyz", $name, 3 * 2, sin(5));# The list contains the scalar values: 11 xyz Reinier 6 -0.958924274663138
(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
print(1, (2, 3), (4 .. 7)); # output: 1234567
Notice that with qw, variable interpolation ( = replacing a variable with the value of that variable) is not possible.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')
($first, $second, $third, $fourth) = ('on') x 4; # output: ('on', 'on', 'on', 'on')
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:$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'
$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'.
($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
The split function is a handy tool to assign string values to variables.($a, $b, $c) = (1, 2); # $c is empty
$str = "23 April 2023";
($day, $month, $year) = split (" ", $str);# the delimiter is a single space: " "
print($month);# $month equals April
or more concise:if (grep{ "Daniel" eq $_ } qw(Sebastian Daniel Florence)) {
print("True\n");
}
else {
print("False\n");
}
or still more compact with a regex (Chapter 9):(grep{ "Daniel" eq $_ } qw(Bastian Daniel Florence)) ? (print("True\n")) : (print("False\n"));
(grep{ /Daniel/ } qw(Bastian Daniel Florence)) ? (print("True\n")) : (print("False\n"));
More on the subroutine 'print' in Chapter 3.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
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");
}
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(reverse(1, (2, 3), (4 .. 7))); # output: 7654321
print(join(":", sort(qw/c b a/)));# output: a:b:c
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
1. How to count distinct elements?
2. How to determine all unique elements that are contained in both lists (intersection)?use List::Util qw(uniq); $count = scalar(uniq qw/1 2 2 3 3 3 4 4 4 4/); print("$count\n"); # Output: 4
3. How to convert list to hash%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
4. how to convert hash to list%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
@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