Programming Language Concepts Perl Lists Arrays Adapted by
Programming Language Concepts Perl Lists & Arrays Adapted by Carl Reynolds from materials by Sean P. Strout 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 1
Lists and Arrays • Plurals are represented by lists and arrays • A list is an ordered collection of scalars (i. e. the data) • An array is a variable that contains a list Indices Values 0 1 2 3 35 12. 4 ’hello’ 1. 72 e 30 4 “byen” • A list can hold any mixture of scalar values 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 2
Accessing Array Elements • Array elements begin at zero and increase by one $fred[0] = “yabba”; $fred[1] = “dabba”; $fred[2] = “doo”; • Arrays use a different namespace than scalars, so variables can have the same names: $fred = 65; print “scalar fred = $fredn”; print “array fred[0] = $fred[0]”; 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 3
Accessing Array Elements • The subscript is truncated to an integer $fred[. 7] ; # $fred[0], or “yabba” • Accessing an element beyond the end of an array yields an undef value (same behavior as scalars) $blank = $fred[ 142_897 ]; # $blank = undef $darl = $mcbride; # unused $mcbride undef 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 4
Special Array Indices • Arrays are automatically extended as needed, so it’s impossible to overrun other data • C vs. Perl example void main() { int x = 10; int array[5]; int i; for (i=0; i<=5; i++) { array[i] = i; } printf(“x=%dn”, x); $x = 10; $array[0] = 0; for ($i=0; $i<=5; $i++) { $array[$i] = $i; } print “x=$xn”; } • Intervening elements are automatically created as undef values. 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 5
Special Array Indices $rocks[0] = ’bedrock’; $rocks[1] = ’slate’; $rocks[2] = ’lava’; $rocks[3] = ’crushed rock’; $rocks[99] = ’schist’; # 95 undef elements • The last element index is $#array_name $rocks = $#rocks; $num_rocks = $rocks + 1 $#rocks = 2 $#rocks = 99 $rocks[ $#rocks ] = ’hard # 99 # ok but # forget # add 97 rock’ # not all new add ideal rocks after lava undef elements this rock at end • Negative indices wrap around $rocks[ -1 ] = ’hard rock’ $dead_rock = $rocks[ -100 ]; $rocks[ -200 ] = ’crystal’ 11/27/2020 # easier way # gets ’bedrock’ # fatal error PLC - Perl Lists & Arrays (v 2. 00) 6
List Literals • A list is represented in your program as a list of comma-separated values enclosed in parentheses. (1, 2, 3) (1, 2, 3, ) (“fred”, 4. 5) () # # list of three values, 1, 2 and 3 trailing comma is ignored two values, “fred” and 4. 5 empty list – zero elements • The range operator creates a series of scalars (1. . 100) # list of 100 ints (5. . 1) # empty list – only counts “uphill” (0, 2. . 6, 10) # 0, 2, 3, 4, 5, 6, 10 ($a. . $b) # range determined by scalar vals (0. . $#rocks) # the indices of the $rocks[] array ($a, 17) # two values, $a and 17 ($b+$c, $d+$e) # two values (“fred”, “barney”, “wilma”, “dino”) # 4 strings 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 7
Quoted Words • Use qw to generate a list of words without typing quote marks qw / fred barney betty wilma dino / • The whitespace is discarded and the elements are treated as single quoted strings – Can’t use n or $scalar_name • How can you make a quoted word list out of these pathnames? Aren’t the forward slashes a problem? /usr/local/pub /home/adjunct/sps You can pick a delimiter for qw other than /: qw{/usr/local/pub /home/adjunct/sps} 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 8
List Assignment • List values can be assigned to scalar variables ($fred, $barney, $dino) = (“flintstone”, “rubble”, undef); $fred = (“a”, “b”, “c”); # bad things! (last in, sticks) ($fred, $barney) = ($barney, $fred); # swapping ($betty[1], $betty[0]) = ($betty[0], $betty[1]); ($fred, $barney) = qw< flintstone rubble slate granite >; ($wilma, $barney) = qw[flintstone]; • Use the at-sign, @ , to refer to the entire array @rocks = qw/ bedrock slate lava /; @tiny = (); # empty list @giant = 1. . 1 e 5; # list of 100, 000 elements @stuff = (@giant, undef, @giant) # 200, 001 elements $dino = “granite”; @quarry = (@rocks, “crushed rock”, @tiny, $dino); # 5 elements 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 9
List Assignment • Arrays cannot contain other arrays, so any assignment from one array to another is a copy. @copy = @quarry # copy a list from one array to another • Use pop to remove the last element from an array @array = 5. . 9; $fred = pop(@array); $barney = pop @array; # # 5 elements 4 elements, $fred = 9 3 elements, $barney = 8 ignore return value • A pop of an empty array returns undef 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 10
List Assignment • Use push to add elements to the end of an array. @array = 5. . 9; $fred = pop(@array); $barney = pop @array; # # 5 elements 4 elements, $fred = 9 3 elements, $barney = 8 ignore return value push(@array, 0); # (5, 6, push(@array, 8); # (5, 6, push(@array, 1. . 10); # add 10 @others = qw/ 9 0 2 1 0 /; push @array, @others; # 5 more 0) 0, 8) more elements to @array: 5 6 0 8 1 2 3 4 5 6 7 8 9 10 9 0 2 1 0 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 11
List Assignment • Use unshift and shift to work on the start of the array. @array = qw/ dino fred barney /; $a = shift(@array); # $a=“dino”, @array has 2 elements $b = shift(@array); # $b = “fred”, @array has 1 element shift @array; # @array is now empty $c = shift @array; # $c gets undef, @array is still empty unshift(@array, 5); # @array has (5) unshift @array, 4; # @array has (4, 5) @others = 1. . 3; unshift @array, @others; # array has (1, 2, 3, 4, 5) • Returns undef for an empty array variable 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 12
Interpolating Arrays into Strings • Arrays can be interpolated into double quoted strings • Elements of the array are automatically separated by the value of the special $” variable (default is space) $” = “, “; @rocks = qw{ flintstone slate rubble }; print “quartz @rocks limestonen”; # output is: quartz flintstone, slate, rubble limestone • Be careful when building strings with the @ symbol $email=“nospam@cs. rit. edu” 11/27/2020 # error! PLC - Perl Lists & Arrays (v 2. 00) 13
Interpolating Arrays into Strings • Is the index variable, $y, interpolated? – No, the index variable is interpreted as an ordinary expression @fred = qw hello dolly ; $y = “ 2*4”; print “This is $fred[$y-1]’s place”; • What happens here? @fred = qw/ eating rocks is wrong /; $fred = “right”; # want to say “this is right[3]”: print “this is $fred[3]n”; print “this is ${fred}[3]n”; print “this is $fred[3]n”; 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) #this is wrong #this is right[3] 14
Foreach Control Structure • Process an entire array in a list using foreach $rock (qw/ bedrock slate lava /) { print “One rock is $rock. n”; } • The control variable is the list element (not a copy) @rocks = qw/ bedrock slate lava /; $rock = “crush”; foreach $rock (@rocks) { $rock = “t$rock”; $rock. = “n”; } print “The rocks are: n”, @rocks; print “$rock=$rockn”; 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) # tab+each+newline # $rock=“crush” 15
Default Variable $_ • The default scalar variable is $_ foreach (1. . 10) { # uses $_ by default print “I can count to $_n”; } • Used as default in many cases $_ = “Yabba doon”; print; # prints $_ by default 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 16
Reverse and Sort • Use reverse to invert the order of a list @fred = 6. . 10; @barney = reverse(@fred); # 10, 9, 8, 7, 6 @wilma = reverse 6. . 10; # same @fred = reverse @fred; # store result back in @fred • Use sort to order a list of values by their ASCII character values: @rocks = qw / b s r g /; @barney = sort(@rocks); @back = reverse sort @rocks; @rocks = sort @rocks; @numbers = sort 97. . 102; 11/27/2020 # # b, g, r, s s, r, g, b stores result back 100, 101, 102, 97, 98, 99 PLC - Perl Lists & Arrays (v 2. 00) 17
Scalar and List Contexts • A given expression means different things depending on the context it is found in 5 + something; sort something; # something must be a scalar # something must be a list @people = qw/ fred barney betty /; # list context @sorted = sort @people; # list context $number = 5 + @people; # scalar context @list = @people; # list of 3 people $n = @people; # the number 3 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 18
Scalar and List Contexts • Here are some more contexts: $fred = something; @pebbles = something; ($wilma, $betty) = something; ($dino) = something; 11/27/2020 # # scalar context list context PLC - Perl Lists & Arrays (v 2. 00) 19
More Scalar and List Contexts #!perl @forwards = qw/ yabba doo/; @backwards = reverse @forwards; print "Quoted forwards list: @forwardsn"; print "Unquoted forwards list: ". @forwards. "n"; ; # yabba doo # 3 print "n"; print "Quoted backwards list: @backwardsn"; print "Unquoted backwards list: ". @backwards. "n"; ; # doo dabba yabba # 3 print "n"; @backwards. List = reverse @backwards; print "Quoted reversed backwards. List: ". "@backwards. List". "n"; print "Unquoted reversed backwards. List: ". @backwards. List. "n"; # yabba doo # 3 print "n"; $backwards. Scalar = reverse @backwards; print "Quoted reversed backwards scalar: ". "$ backwards. Scalar". "n"; # abbayabbadood print "Unquoted reversed backwards. Scalar: ". $backwards. Scalar. "n"; 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) # abbayabbadood 20
Scalar and List Contexts • What are these contexts? $fred[something] = something; push @fred, something; foreach $something 1 (something 2) { … } 123 + something; if (something) { … } print something; 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 21
Scalar and List Contexts • To force a scalar context when Perl is expecting a list, use the fake function scalar @rocks = qw/ talc quartz jade obsidian /; print “How many rocks do you have? n”; print “I have “, @rocks, “ rocks!n”; # incorrect print “I have “, scalar @rocks, “ rocks!n”; # correct 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 22
Revision History • Revision History – v 1. 00, 10/10/2003 5: 25 PM, sps Initial revision. -- v 2. 0, 1/9/2005, chr 11/27/2020 PLC - Perl Lists & Arrays (v 2. 00) 23
- Slides: 23