Lists and Arrays
Lists
- Lists are ordered collection of scalar values or variables
- Used in many ways: stored in arrays, used to create hashes
- stored in arrays
- used in list assignment
- used to create hashes
- function parameters
- function return values
- First study lists, then study lists stored as arrays
Creating Lists
- Lists are formed using comma which is the list operator
- Four example lists (not used for anything):
1,2,3
'a', 'b', 'c', 1,'aa'
'aa', 'bb', 'cc'
qw(aa bb cc) # qw means quote word
the last 2 are identical
Like C, Perl allows a comma at the end of a list: print 1,2,3, ;
print 1,2,3, ;
Lists and Arrays
- Lists can be stored in array variables
@a = (11, 22, 33);
print @a; # 11223344
print @a[1]; # 22
print $#a; # prints 3, the index of the last element in the list
print $a[$#a]; # what does this do?
print $a ; # $a is undefined
Arrays have Value Semantics
- Arrays don't act like references:
@a = (1,2,3);
@b = @a;
$a[1] = 99;
print @a; # 1993
print @b; # 123
Printing Lists with Join
print 1,2,3; # 123 - No separator
print join '+', 1,2,3; # 1+2+3 - Join using + as separator
print join '+', reverse 1,2,3; # 3+2+1
@a = (1,2,3); # Parens required
print @a; # 123 - No separator
print join '+', @a; # 1+2+3 - Join using + as separator
print join '+', reverse @a; # 3+2+1
Foreach loop: More printing lists
- foreach iterates through the list
@a = (11, 22, 33);
foreach $x (@a)
{
print $x;
}
foreach (@a) {print $_;}
foreach (@a) {print ;}
for (@a) {print;}
$x
is local to the loop
Lists and Parentheses
- Sometimes precedence requires parentheses to get what you want
- Example:
@a = 1, 2, 3;
print @a; # 1
@b = (1, 2, 3);
print @b; # 123
Assignment operator (=) has higher precedence than list forming operator (,)
The 2 and 3 are ignored in the assignment to @a
Right and Left Precedence
- List operators have different right and left precedence:
print 1, 2, sort 4, 6, 3, 5;
sort higher than terms to it's left, so it's done first
sort low with respect to terms on right , so it's done last
The Empty List
- The empty list is represented as
()
Lists Within Lists
- Nested lists are flattened
- Example:
(1,(2,3)) == (1,2,3)
- (== works on lists)
- This is important for subroutine parameters
- A list of lists can be created as a list of references to lists, which we'll see later
Lists Assignment
- Lists of variables can appear on the left side of an assignment
- Examples:
($a, $b) = (1, 2)
($a, $b, c) = (1, 2) # c is uninitialized
($a) = (1, 2) # 2 is ignored
This is important in function parameters and return values
Splitting a String to Create a List
$s = "abcde";
print $s[0]; # ERROR - s is uninitialized
How to create a list from a string
$s = "abc de";
@a = split (//, $s);
print $a[0]; # a
print "|", (join ",", @a), "|"; # |a, b, c, , d, e|
Why are the parens needed?
List/Array Indexing
- Lists and arrays can be indexed:
print( (1,2,3,4,5)[1] ); # 2
print( (1,2,3,4,5)[-1] ); # 5
@a = (1, 2, 3, 4, 5);
print $a[2]; # 3
print $a[-2]; # 4
List Operators
- range operator (1..5) == (1,2,3,4,5)
for $i (1..5) {print $i;}
for (1..5) {print ;}
map: Operate on each element of a list
@a = (-5, -6, -7, -8);
@b = map {abs $_} @a;
print @b; # 5678
split:
$a = "one two";
($b, $c) = split / /, $a;
print $c, $b;
sort: print sort 4,2,5,1; # 1245
reverse: print reverse 1,2,3,4; # 4321
grep: return a list of elements that match
print join ",", grep /2/, 14, 21, 5, 92; # 21,92
print grep !/2/, 4, 2, 5, 1; # 451
Output Field and Record Separators
- Output record terminators and separator can be redefined
- $, - output field separator:
- printed between list elements
- default ""
- $\ - output record terminator:
- printed at end of list
- default ""
- Example
$, = "a";
$\ = "x";
print 1,2,3,4; # 1a2a3a4x
Arrays as Stacks and Queues
Stack ops: push, pop
- Assume top of stack is right side of list
- Push adds to right end
- Pop removes right end and returns it
@a = (11,12,13);
$x = pop @a; # remove from back of list
print join ', ', @a; # 11, 12
print $x; # 13
push @a, (55, 56); # add to back of list
print join ', ', @a; # 11, 12, 55, 66
- Queue ops: push and shift
- Assume front of queue is left is and back is right
- Push adds to right end
- Shift removes leftmost element of list and returns it
unshift adds to left of list
Could use other ends, of course
Shift used in handling function arguments
Context
CONTEXT: determines how some expressions are evaluated
- Two contexts: list and scalar
- Example:
@a = (5,6,7,8);
$x = @a; # @a is evaluated in scalar context
@y = @a; # @a is evaluated in list context
print $x; # Prints 4, the length of the array @a
print @y; # Prints 5678, the elements of @a
- Context affects:
- a variable - how it is interpreted,
- function results - how they are interpreted
- operators - what returned
- Context is set by:
- LHS of assignment sets the context for the RHS,
- what function expects sets the context for its parameters
- boolean expression - never cause any conversions
- subroutines can return different things in different contexts
- print scalar reverse $line