CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

24
CPTG286K Programming - Perl Chapter 3: Arrays and List Data

Transcript of CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Page 1: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

CPTG286K Programming - Perl

Chapter 3: Arrays and List Data

Page 2: 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

Page 3: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

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

Page 4: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

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

Page 5: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Examples of List Constructor Operators

(1..5) # same as (1, 2, 3, 4, 5)

(1.2 .. 5.2) # same as (1, 2, 3, 4, 5)

(1.3 .. 6.1) # 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 ”,@a,“\n”);

# function print’s list literal

Page 6: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Array Variables

• Array variables hold a single list value

• Variable names for arrays are similar to those for scalars, except they start with a @ rather than a $

• The value of an un-assigned array variable is (), the empty list

• @fred is NOT related to $fred

Page 7: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

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

Page 8: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Examples of Assigning Arrays

@fred = (1,2,3); # @fred is (1,2,3)

@barney = @fred; # Copied to barney array

@huh = 1; # Scalar 1 is single # element value of huh

@fred = qw(one two); # @fred is (“one”,“two”)

@barney = (4,5,@fred,6,7) # barney becomes

# (4,5,“one”,“two”,6,7)

@barney = (8,@barney); # (8,4,5,“one”,“two”,6,7)

@barney = (@barney,“last”);

# (8,4,5,“one”,“two”,6,7,“last”)

Page 9: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

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

Page 10: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Intro to Scalar and Array Context

• Scalar context: a scalar variable is assigned an array variable. The scalar variable yields the array’s length:

@fred = (4,5,6); # Initialize @fred

$a = @fred; # $a gets 3, the length of @fred

• Array (or list) context: an array variable receives another array variable:

($a) = @fred; # $a gets first element of @fred

Page 11: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

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!($d,@fred) = ($a,$b,$c); # $d = $a, @fred = ($b,$c)($e,@fred) = @fred; # Remove @fred[0] to $e, so

# $e = $b, @fred = ($c)

• Note: Place the array variable at the end of the list, because it may contain an undetermined number of values

@fred) = (4,5,6); # Initialize @fred$a = @fred; # scalar context: $a on left

# $a gets 3, length of @fred($a) = @fred; # array context: ($a) on left

# $a gets first element of @fred

Page 12: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Array Element Access

• Elements are accessed as scalar variables; the first element of @fred 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 in @array_name

Page 13: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Array Element Access Examples

@fred = (7,8,9); # Initialize @fred

$a = 2;

$b = $fred[0]; # $b is 7

$b = $fred[$a]; # same as $fred[2], $b is now

$fred[0] = 5; # @fred = (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 elements

@fred = (“fred”, “wilma”, “pebbles”, “dino”);

print $fred[-1]; # prints “dino”

print $#fred; # prints 3

print $fred[$#fred]; # prints “dino” too

Page 14: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Array Element Access Using A

Page 15: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Slice access

• Use the @ prefix instead of $, because you’re accessing a part of an array, which is more than just one scalar element

@fred[0,1]; # same as ($fred[0],$fred[1])@fred[0,1] = @fred[1,0]; # swap first two elements@fred[0,1,2] = @fred[1,1,1];

# make all 3 elements like the second@fred[1,2] = (9,10);

# change last two values to 9 and 10$a = 2; # set $a to 2

($c) = (7,8,9)[$a-1]; # $c = 8

Page 16: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Examples of Slice Access

@fred[0,1]; # or ($fred[0],$fred[1])

@fred[0,1] = @fred[1,0]; # swap first two elements

@fred[0,1,2] = @fred[1,1,1];

# make all 3 elements like the second

@fred[1,2] = (9,10); # change last two values

# to 9 and 10

@fred = (7,8,9); # Initialize @fred

($c) = (7,8,9)[$a-1]; # same as $fred[1], $c = 8

@barney = (2,1,0); # Setup subscript array

@backfred = @fred[@barney];

# yields @fred[2,1,0], or (9,8,7)

Page 17: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Out of range access

• Assigning a value beyond the end of array, automatically extends it and assigns undef to existing intermediate values

@fred = (1,2,3,“hi”); # Initialize @fred

$fred[6] = “ho”; # @fred is

#(1,2,3,“hi”, undef, undef, “ho”)

• An undef value is returned for array elements accessed beyond the end of array

@barney = $fred[7]; # $barney is undef

Page 18: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

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 list

@somelist = (8,’eight’,9,’nine’);

@tenlist = (10,’ten’);

push(@somelist, @tenlist);# @somelist is now

# (8,’eight’,9,’nine’,10,’ten’);

unshift(@somelist,’The Big Seven’);

# (‘The Big Seven’,8,’eight’,9,’nine’,10,’ten’);

Page 19: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Pop and Shift functions

• The pop function removes elements from the end of list; the shift function removes elements from the beginning of list

@somelist = (8,’eight’,9,’nine’,10);

pop (@somelist); # @somelist is now

# (8,’eight’,9,’nine’);

$removed = shift (@somelist);

# $removed = 8

# #somelist is now

# (’eight’,9,’nine’);

Page 20: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Reverse function

• The reverse function reverses the order of elements in the list

@a = (7,8,9); # Initialize @a

@b = reverse(@a); # @b is (9,8,7)

# @a is unchanged

• The argument list is unaltered by the function unless it is assigned to the same variable

@b = reverse(@b); # @b is reverse of itself

Page 21: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Sort function

• The sort function sorts its arguments as single strings in ascending ASCII order

• A sorted list is returned without altering the original list

@x = sort(“small”, “medium”, “large”);

# @x is “large”, “medium”, “small”

@y = (1,2,4,8,16,32,64);

@y = sort(@y);# performs ASCII sort

# @y is 1,16,2,32,4,64,8

Page 22: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Chomp and STDIN in list context

• The chomp function also works on array variables to remove the newline character from each array element

@stuff = (“hello\n”,”world\n”,”happy days”);

chomp(@stuff);

# @stuff is now (“hello”,”world”,”happy days”)

• <STDIN> in list context can read remaining lines up to end of file

@a = <STDIN>; # read until EOF, or CTRL-D (Z)

Page 23: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

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

Page 24: CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

Examples of Array Interpolation

@fred = (“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 right[1]”

@fred = (“a”,”bb”,”ccc”,1,2,3);

@all = “Now for @fred[2,3] here!”;

# $all gets “Now for ccc 1 here!”

@all = “Now for @fred[@fred[4,5]] here!”; # same