Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.

Similar presentations


Presentation on theme: "Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with."— Presentation transcript:

1 Perl Arrays and Lists Software Tools

2 Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with the elements separated by commas (and optional spaces). (1,2, 3,4.1) l List elements can be constants or expressions: ("Bill", 4, "pie", "B. Gates") ($num, 17, $num+1+$i) l Memory for lists is dynamically allocated and removed as the program runs.

3 Slide 3 Lists l The empty list (no elements) is represented by an empty pair of parenthesis: ( )# empty list The list constructor “.. ” creates a list of values with increments of 1: (1.. 4) # same as (1, 2, 3, 4) ( ) # same as (1, 2, 3, 4, 5) ??!! (1, 5.. 7) # same as (1, 5, 6, 7) ($min.. $max) # depends on values of $min and $max (10.. 5) # same as ( ) -> it can’t count down

4 Slide 4 Single-Word Lists l There is a shortcut for lists of single-word strings, the “quote word” function: ("bill", "gates", "pie", "toss") # usual version qw(bill gates pie toss) # same as above qw(bill gates pie toss) # also okay

5 Slide 5 Arrays l An array contains a list (zero or more scalar values). Array variable names are similar to scalar variable names, except the initial character is ” instead of “ $ = (1,2, copies = ("Bill", 4, "pie", "B. Gates"); $num = = ($num, 17, $num+1); l If a scalar value is assigned to an array variable, it becomes a single-element list = 4;# becomes (4) automatically

6 Slide 6 Inserting Arrays l You can also insert array elements into = = (1, 10); # = # = 99); # (0,1,2,6,7,8,10,99) l Note that the inserted array elements are at the same level as the other elements, not in a “sub-list”.

7 Slide 7 Left-Side Assignment If a list only contains variables, you can use it on the left side of an assignment: ($a,$b,$c) = (1,2,3); # set $a=1, $b=2, $c=3 ($a,$b) = ($b,$a); # swap $a and $b = ($a,$b,$c); # set $d=$a # remove first element # and put it in $e # end up with: $a=2, $b=1, $c=3 # $d=2, An array variable can only occur in the last position in the list, because the array variable is “greedy” and consumes all the remaining values.

8 Slide 8 Array Length If an array variable is assigned to a scalar variable, the number assigned is the length of the = (1,2,3); $n # $n gets 3, the length The context determines whether the length of the array is used or the list: $n $n gets the length ($n) # $n gets the first element The first assignment is a scalar assignment, is treated as a scalar, returning its length. The second assignment is an array assignment, and gives the first element (silently discarding the rest).

9 Slide 9 Array Subscripting l Each element of an array can be accessed by its integer position in the list. l The first element starts at position 0 (like C++ arrays). The first element of array is accessed as = qw(bill gates pie toss); $name1 = $a[0]; # sets name1 to "bill" $name2 = $a[3]; # sets name2 to "toss" $a[1] = "clinton"; # a: qw(bill clinton pie toss) Note that on the array name becomes a $ when accessing individual elements.

10 Slide 10 Array Subscripting You can use all the usual scalar operations on the array = (1,2,3); $a[0]++;# a: (2,2,3) $a[1] += 4;# a: (2,6,3) $a[2] += $a[0];# a: (2,6,5) # swap the first two elements ($a[0],$a[1]) = ($a[1],$a[0]); # a: (6,2,5)

11 Slide 11 Array Slices l Accessing a list of elements from the same array is called a slice. l Perl provides a special shortcut for = # swap the first two # make all 3 elements like the = (7,4); # change the last two to 7 and 4 # a: (1,7,4) Note that slices rather than $. This is because slices work with lists rather than scalar values.

12 Slide 12 List Slices l Slices also work directly on lists: $c = (1,2,3,4,5)[2];# sets $c to = (1,2,3,4,5)[2,4];# to (3,5) n The second statement above is equivalent =

13 Slide 13 Index Expressions l You can use expressions for the subscripts just like in = (5,6,7); $n = 2; $m = $list1[$n];# $m = 7 $p = $list1[$n-1];# $p = 6 ($p) = (5,6,7)[$n-1];# same thing using slice

14 Slide 14 Slice Expressions l You can use also array expressions to index slices if you want to be = = = # same # or ($nums[2], $nums[1], $nums[0]) # or (7,6,5)

15 Slide 15 Bad Subscripting If you access an array beyond the end of the array, the undef value is returned without warning. undef is 0 when used as a number, the empty string when used as a = (5,6,7); $nums[3] = "bill"; (5,6,7,"bill") $nums[5] = "g."; (5,6,7,"bill",undef,"g.") l Assignment to an array element with a subscript less than zero is a fatal error.

16 Slide 16 Backward Subscripting You can use $#bill to get the index value of the last element Accessing an array with a negative subscript counts back from the end. So, another way to get the last element is = qw(cheap rich lucky likespie); print $#bill;# prints 3 print $bill[$#bill];# prints likespie print $bill[$#bill-1];# prints lucky print $bill[-1];# prints likespie print $bill[-2];# prints lucky print $bill[-3];# prints rich

17 Slide 17 push and pop You can use push and pop to add and remove values from the end of an = (1,2,3); $new = 6; # same = # so far: (1,2,3,6) $oldvalue = # removes last element # so far: (1,2,3) # can push multiple values # so far: (1,2,3,4,5,6) pop returns undef if given an empty array.

18 Slide 18 unshift and shift You can use unshift and shift to add and remove values from the beginning of an = (1,2,3); $new = 6; # same = # so far: (6,1,2,3) $old = # removes first element # so far: (1,2,3) # can unshift multiple values # same = # so far: (4,5,6,1,2,3) shift returns undef if given an empty array.

19 Slide 19 reverse and sort The reverse function reverses the array, returning the resulting = = = reverse(1,2,3);# same The sort function returns a sorted array in ascending ASCII = qw(small medium = # large, medium, = sort(qw(small medium large)); # =

20 Slide 20 Array Variable Interpolation Array elements can be interpolated in double-quoted = qw(unix shell perl); print "$comp111[0] programming is mainly done "; print "in $comp111[2]\n"; $n=3; print "$comp111[0] programming is mainly done "; print "in $comp111[$n-1]\n";# same thing Arrays and array slices will be interpolated (printed) with spaces between the = (1,2,"bill",3); print prints a: 1 2 bill 3 print prints a1: 2 bill

21 Slide 21 Scalar and List Context If an operator or function expects a scalar argument, the argument is evaluated in a scalar context. $n $n gets the length If an operator or function expects a list argument, the argument is evaluated in a list context. ($n) # $n gets the first element A scalar value used within a list context is promoted to a single-element = 1; = (1)

22 Slide and $ (at sign) Refers to the entire array or slice of an array (when used with [ ]). $ (dollar sign) Refers to one element of the array, used with [ ]

23 Slide 23 Array chomp The chomp function also works for array variables, removing any ending newlines in each element = qw(unix\n shell\n perl); is now qw(unix shell perl) Array chomp is especially useful when reading a file or input from a user. in a list context will return all remaining lines up to the end of the file, or until the user hits = ;# read input in a list context remove all trailing newlines

24 Slide 24 Example: Max Value How to find the biggest value in an array of = (23,4,56,99,36,24); $max = $a[0]; for($i=1; $i++){ if($a[$i] > $max){ $max = $a[$i]; } print "max is: $max\n";


Download ppt "Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with."

Similar presentations


Ads by Google