CPTG286K Programming - Perl Chapter 3: Arrays and List Data
Lists and Arrays A list is ordered scalar data An array variable holds a list Each array element is a scalar variable with an independent and ordered scalar value PERL arrays can have no elements, or can fill up to all of available memory
Literal Representation An empty list is represented by an empty pair of parentheses: () Non-empty lists contain comma-separated values enclosed in parentheses: (1,2,3)# array of 1, 2, and 3 (“fred”, 4.5)# array of “fred” and 4.5 List elements may contain expressions: ($a,17)# current value of $a and 17 ($b+$c,$d+$e)# two values from 4 variables
List Constructor Operator A list constructor operator creates a list of values List literals that contain two scalar values separated by two consecutive periods create a list of values Values start at left scalar value through the right scalar value, incrementing by one
Examples of List Constructor Operators (1..5)# same as (1, 2, 3, 4, 5) ( )# same as (1, 2, 3, 4, 5) ( )# same as (1, 2, 3, 4, 5, 6) (2..6,10,12)# same as (2,3,4,5,6,10,12) ($a..$b)# range determined by $a, $b # if $a > $b then empty list () (5..1)# same as () print (“The answer is # function print’s list literal
Array Variables Array variables hold a single list value Variable names for arrays are similar to those for scalars, except they start with rather than a $ The value of an un-assigned array variable is (), the empty is NOT related to $fred
Assigning Values Into Arrays Use the equal sign = to assign values into array variables Array variable names may appear in a list of literals (not same as list of lists); variable names are replaced by their current values If a scalar value is assigned to an array variable, the value becomes the single element of an array
Examples of Assigning = is Copied to barney = 1;# Scalar 1 is single # element value of = qw(one is = barney becomes # = = # (8,4,5,“one”,“two”,6,7,“last”)
Variable References If a list literal contains only variable references such as ($a, $b, $c), rather than expressions such as ($a+$b, $c-1, $d*$z), the references can be treated as variables If number of elements does not match number of variables, excess values on the right side of equal sign are discarded; excess variables are assigned undef
Intro to Scalar and Array Context Scalar context: a scalar variable is assigned an array variable. The scalar variable yields the array’s = (4,5,6);# $a $a gets 3, the length Array (or list) context: an array variable receives another array variable: ($a) $a gets first element
Examples of Variable References ($a,$b,$c) = (1,2,3);# assign 1,2,3 to $a, $b, $c ($a,$b) = ($b,$a);# swap with no tmp variable! = ($a,$b,$c);# $d = = ($b,$c) to $e, so # $e = = ($c) Note: Place the array variable at the end of the list, because it may contain an undetermined number of = (4,5,6);# $a scalar context: $a on left # $a gets 3, length ($a) array context: ($a) on left # $a gets first element
Array Element Access Elements are accessed as scalar variables; the first element is accessed as $fred[0] A negative subscript counts elements from the end: -1 is last element, second to last is -2, etc. $#array_name yields the index value of the last element
Array Element Access = (7,8,9);# $a = 2; $b = $fred[0];# $b is 7 $b = $fred[$a];# same as $fred[2], $b is now $fred[0] = = (5,8,9); $c = $fred[1];# $c is 8 $fred[2]++;# increment third element $fred[1] += 4;# add 4 to second element ($fred[0],$fred[1]) = ($fred[1],$fred[0]); # swap the first two = (“fred”, “wilma”, “pebbles”, “dino”); print $fred[-1];# prints “dino” print $#fred;# prints 3 print $fred[$#fred];# prints “dino” too
Array Element Access Using A
Slice access Use prefix instead of $, because you’re accessing a part of an array, which is more than just one scalar same as # swap first two # make all 3 elements like the = (9,10); # change last two values to 9 and 10 $a = 2;# set $a to 2 ($c) = (7,8,9)[$a-1];# $c = 8
Examples of Slice or # swap first two # make all 3 elements like the = (9,10);# change last two values # to 9 and = (7,8,9);# ($c) = (7,8,9)[$a-1];# same as $fred[1], $c = = (2,1,0);# Setup subscript = # or (9,8,7)
Out of range access Assigning a value beyond the end of array, automatically extends it and assigns undef to existing intermediate = (1,2,3, “ hi”);# $fred[6] = “ho”; is #(1,2,3,“hi”, undef, undef, “ho”) An undef value is returned for array elements accessed beyond the end of = $fred[7]; # $barney is undef
Push and Unshift functions The push function adds elements to the end of the list; the unshift function adds elements to the beginning of the = is now # (8,’eight’,9,’nine’,10,’ten’); Big Seven’); # (‘The Big Seven’,8,’eight’,9,’nine’,10,’ten’);
Pop and Shift functions The pop function removes elements from the end of list; the shift function removes elements from the beginning of = (8,’eight’,9,’nine’,10); is now # (8,’eight’,9,’nine’); $removed = shift # $removed = 8 # #somelist is now # (’eight’,9,’nine’);
Reverse function The reverse function reverses the order of elements in the = is (9,8,7) is unchanged The argument list is unaltered by the function unless it is assigned to the same = is reverse of itself
Sort function The sort function sorts its arguments as single strings in ascending ASCII order A sorted list is returned without altering the original = sort(“small”, “medium”, “large”); is “large”, “medium”, = = performs ASCII sort is 1,16,2,32,4,64,8
Chomp and STDIN in list context The chomp function also works on array variables to remove the newline character from each array = (“hello\n”,”world\n”,”happy days”); is now (“hello”,”world”,”happy days”) in list context can read remaining lines up to end of = ;# read until EOF, or CTRL-D (Z)
Variable Interpolation in Arrays Arrays may be interpolated into double- quoted strings Use the curly bracket delimitter to distinguish scalar variable placed next to square brackets, from array variables Portions of an array can be interpolated by using slices
Examples of Array = (“hello”, “dolly”); $y = 2; $x = “This is $fred[$y-1]’s place”; # This is dolly’s place $fred = “right”; $y = “This is $fred[1]”; # $y=“This is dolly” $y = “This is ${fred}[1]”; # “This is = = “Now here!”; # $all gets “Now for ccc 1 = “Now for here!”; # same