Lists and Arrays

This lecture notes is based on Prof Doreen De Leon's lectures.

Reading

Learning Perl, Chapter 3.

Definition

Examples

@names=("Amy", "Ben", "Charles", "Dianne");
# Amy Ben Charles Dianne
for($i=0; $i < 4; $i++) {
    print $names[$i], " ";
}
print "\n";
# an array of numbers
@grades = (99, 22, 35, 85, 63);
print $grades[2], "\n"; # 35
# a mix of numbers and string
@array = (15, "Hi", "hello", 23.5);
print $array[0], $array[1], "\n"; # 15Hi
$name0 = "Amy";
$name1 = "Ben";
$name2 = "Charles";
# an array of variables
@names = ($name0, $name1, $name2);
print @names, "\n"; #AmyBenCharles
@names2 = ("Hi!".$name0, "Hi!".$name1, "Hi!".$name2);
print @names2, "\n"; #Hi!AmyHi!BenHi!Charles

Variables

Accessing Array Elements

Examples

# no need to declare an array
$num[0] = 11;
$num[1] = 12;
$num[2] = 20;
print $num[1],"\n";
# num is a seperate variable;
$num = 100;
print $num,"\n";
$num[5] = 50;
$num[0] = 11;
$num[1] = 12;
$num[2] = 20;
print $num[-1], "\n"; # 20
print $num[-2], "\n"; # 12
$num[5] = 50;
print $num[-1], "\n"; # 50
print $num[-2], "\n"; # undefined
$num[0] = 11;
$num[1] = 12;
$num[2] = 20;
$num[100] = -55;
$lastIndex = $#num; # index of the last element
print $lastIndex, "\n"; # 100;
print $num[$lastIndex], "\n"; # -55;
$#num=2; # forget about anything after index 2
print @num; #111220

The Range Operator

Examples

@a = (1..5); # same as (1,2,3,4,5)
print @a; # 12345
print "\n";
@b = (1.1..5.7); # same as before
print @b; # 12345
print "\n";
@c = (1..3, 10..12, 15, 20); # (1,2,3,10,11,12,15,20)
print @c;
print "\n";
@d = ('a'..'e'); #('a','b','c','d',e');
print @d; #abcde
print "\n";
$x = 10;
$y = 13;
@e=($x..$y); # (10,11,12,13);
print @e; # 10111213
print "\n";
@f=(5..2); #empty
print @f;
print "\n";

qw shortcut

Example

@a = ('Amy', 'Ben','Charles', 'Dianne');
for($i=0; $i <= $#a; $i++) {
    print $a[$i], " ";
}
print "\n";
# same as @a
@b = qw/ Amy Ben Charles Dianne/;
for($i=0; $i <= $#b; $i++) {
    print $b[$i], " ";
}
#same as @a
print "\n";
@c = qw! Amy Ben Charles Dianne!;
for($i=0; $i <= $#c; $i++) {
    print $c[$i], " ";
}
print "\n";

@c = qw# Amy Ben Charles Dianne#;
@c = qw( Amy Ben Charles Dianne);
@c = qw< Amy Ben Charles Dianne>;
@c = qw[ Amy Ben Charles Dianne];
@c = qw{ Amy Ben Charles Dianne};

$str1 = "Apple";
$str2 = "Lemon";
@a = ($str1, $str2);
print @a, "\n"; # AppleLemmo
@b =qw/$str1 $str2/;
print @b, "\n"; # $str1$str2
               # single quote

List Assignment

($a, $b, $c) = ('Apple', 'Banana', 'Orange');
print $a, " ",$b,"\n"; # Apple Banana
($a, $b) = ($b, $a); #swap
print $a, " ",$b, "\n"; # Banana Apple
($a, $b) = ("Lemon", "Pear", "Mango"); #extra item will be ignored
print $a, " ",$b, "\n"; # Lemon Pear
($a, $b) = ("Nactarine"); # $b is undef
@fruit1 = ('Apple', 'Banana', 'Orange');
@fruit2 = ('Lemon', 'Pear', 'Mango');
$a = 'Nactarine';
@fruit = (@fruit1, $a, @fruit2, 'Peach');
for($i = 0; $i < 8; $i++) {
    print $fruit[$i], " ";
}
@fruit1 = ('Apple', 'Banana', 'Orange');
@fruit2 = @fruit1; #copy an array
$fruit2[2] = 'Mango';
print @fruit1;
print "\n";
print @fruit2;

Interpolation of Arrays

Example

@array = (1,3,5,7,9);
#The array is 13579. No space
print "The array is ", @array, ".\n"; 
#The array is 1 3 5 7 9. With space
print "The array is @array.\n"; 
@array = qw(apple orange banana);
$array= "abc"; 
print "I like $array[1].\n"; # I like orange
print "I like ${array}[1].\n"; # I like abc[1].
print "My E-mail address is charles\@abc.com";

Array Operators

@array = qw(a b c d e);
print "@array\n"; # a b c d e 
$x = pop(@array); 
print $x, "\n"; # e
print "@array\n"; # a b c d
$y = pop @array;
print $y, "\n"; #d;
print "@array\n"; # a b c
pop @array; 
print "@array\n"; # a b
push(@array, 'h'); 
print "@array\n"; # a b h
push(@array, 'x', 'y');
print "@array\n"; # a b h x y
push @array, 'A';
print "@array\n"; # a b h x y A
push @array, 'C'..'F';
print "@array\n"; # a b h x y A C D E F
@array1 = qw(1 2 3);
@array2 = qw(2 3 5);
push @array1, @array2;
print "@array1\n"; # 1 2 3 2 3 5
@array = qw(a b c d e);
print "@array\n"; # a b c d e 
$x = shift(@array); 
print $x, "\n"; # a
print "@array\n"; # b c d e
$y = shift @array;
print $y, "\n"; #b;
print "@array\n"; # c d e
shift @array; 
print "@array\n"; # d e
unshift(@array, 'h'); 
print "@array\n"; # h d e 
unshift(@array, 'x', 'y');
print "@array\n"; # x y h d e
unshift @array, 'A';
print "@array\n"; # A x y h d e
unshift @array, 'C'..'F';
print "@array\n"; # C D E F A x y h d e 
@array1 = qw(1 2 3);
@array2 = qw(2 3 5);
unshift @array1, @array2;
print "@array1\n"; # 2 3 5 1 2 3 
@array = 2..5;
print @array; #2345
print "\n";
@array2 = reverse(@array);
# or @array2 = reverse @array
print @array; #2345
print "\n";
print @array2; #5432
@names = qw(charles betty amy eve dave);
@sorted = sort(@names);
# or @sorted = sort @names
print "@sorted";
@num = 97..102;
@sorted = sort @num;
# 100 101 102 97 98 99
print "@sorted";

The foreach Control Structure

@array = qw(2 3 5 7 11 13);
$sum = 0;
foreach $number (@array) {
   $sum+=$number
}
print "The sum is $sum.\n"; #41
@array = qw(2 3 5 7 11 13);
$sum = 0;
foreach (@array) {
   $sum+=$_
}
print "The sum is $sum.\n"; #41

The Default Variable: $_

$_ = "hello world";
print; 
# hello world