1 / 23

Programming Language Concepts Perl Lists & Arrays

Programming Language Concepts Perl Lists &amp; Arrays. Adapted by Carl Reynolds from materials by Sean P. Strout. Values. 0. 35. 1. 12.4. 2. ’hello’. Indices. 1.72e30. 3. 4. “bye<br>”. Lists and Arrays. Plurals are represented by lists and arrays

Télécharger la présentation

Programming Language Concepts Perl Lists &amp; Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Language ConceptsPerl Lists & Arrays Adapted by Carl Reynolds from materials by Sean P. Strout PLC - Perl Lists & Arrays (v2.00)

  2. Values 0 35 1 12.4 2 ’hello’ Indices 1.72e30 3 4 “bye\n” 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 • A list can hold any mixture of scalar values PLC - Perl Lists & Arrays (v2.00)

  3. 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 = $fred\n”; print “array fred[0] = $fred[0]”; PLC - Perl Lists & Arrays (v2.00)

  4. 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 PLC - Perl Lists & Arrays (v2.00)

  5. Special Array Indices • Arrays are automatically extended as needed, so it’s impossible to overrun other data • C vs. Perl example void main() { $x = 10; int x = 10; $array[0] = 0; int array[5]; for ($i=0;$i<=5;$i++) { int i; $array[$i] = $i; } for (i=0; i<=5; i++) { print “x=$x\n”; array[i] = i; } printf(“x=%d\n”, x); } • Intervening elements are automatically created as undef values. PLC - Perl Lists & Arrays (v2.00)

  6. 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; # 99 $num_rocks = $rocks + 1 # ok but not ideal $#rocks = 2 # forget all rocks after lava $#rocks = 99 # add 97 new undef elements $rocks[ $#rocks ] = ’hard rock’ # add this rock at end • Negative indices wrap around $rocks[ -1 ] = ’hard rock’ # easier way $dead_rock = $rocks[ -100 ]; # gets ’bedrock’ $rocks[ -200 ] = ’crystal’ # fatal error PLC - Perl Lists & Arrays (v2.00)

  7. List Literals • A list is represented in your program as a list of comma-separated values enclosed in parentheses. (1, 2, 3) # list of three values, 1, 2 and 3 (1, 2, 3,) # trailing comma is ignored (“fred”, 4.5) # 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 PLC - Perl Lists & Arrays (v2.00)

  8. 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} PLC - Perl Lists & Arrays (v2.00)

  9. 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..1e5; # list of 100,000 elements @stuff = (@giant, undef, @giant) # 200,001 elements $dino = “granite”; @quarry = (@rocks, “crushed rock”, @tiny, $dino); # 5 elements PLC - Perl Lists & Arrays (v2.00)

  10. 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; # 5 elements $fred = pop(@array); # 4 elements, $fred = 9 $barney = pop @array; # 3 elements, $barney = 8 pop @array; # ignore return value • A pop of an empty array returns undef PLC - Perl Lists & Arrays (v2.00)

  11. List Assignment • Use push to add elements to the end of an array. @array = 5..9; # 5 elements $fred = pop(@array); # 4 elements, $fred = 9 $barney = pop @array; # 3 elements, $barney = 8 pop @array; # ignore return value push(@array, 0); # (5, 6, 0) push(@array, 8); # (5, 6, 0, 8) push(@array, 1..10); # add 10 more elements @others = qw/ 9 0 2 1 0 /; push @array, @others; # 5 more elements to @array @array: 5 6 0 8 1 2 3 4 5 6 7 8 9 10 9 0 2 1 0 PLC - Perl Lists & Arrays (v2.00)

  12. 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 PLC - Perl Lists & Arrays (v2.00)

  13. 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 limestone\n”; # output is: quartz flintstone, slate, rubble limestone • Be careful when building strings with the @ symbol $email=“nospam@cs.rit.edu” # error! PLC - Perl Lists & Arrays (v2.00)

  14. 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”; #this is wrong print “this is ${fred}[3]\n”; #this is right[3] print “this is $fred\[3]\n”; #this is right[3] PLC - Perl Lists & Arrays (v2.00)

  15. Foreach Control Structure • Process an entire array in a list using foreach 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; # tab+each+newline print “\$rock=$rock\n”; # $rock=“crush” PLC - Perl Lists & Arrays (v2.00)

  16. 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 dabba doo\n”; print; # prints $_ by default PLC - Perl Lists & Arrays (v2.00)

  17. 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); # b, g, r, s @back = reverse sort @rocks; # s, r, g, b @rocks = sort @rocks; # stores result back @numbers = sort 97..102; # 100,101,102,97,98,99 PLC - Perl Lists & Arrays (v2.00)

  18. Scalar and List Contexts • A given expression means different things depending on the context it is found in 5 + something; # something must be a scalar sort something; # 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 PLC - Perl Lists & Arrays (v2.00)

  19. Scalar and List Contexts • Here are some more contexts: $fred = something; # scalar context @pebbles = something; # list context ($wilma, $betty) = something; # list context ($dino) = something; # list context PLC - Perl Lists & Arrays (v2.00)

  20. More Scalar and List Contexts #!perl @forwards = qw/ yabba dabba doo/; @backwards = reverse @forwards; print "Quoted forwards list: @forwards\n"; # yabba dabba doo print "Unquoted forwards list: " . @forwards . "\n";; # 3 print "\n"; print "Quoted backwards list: @backwards\n"; # doo dabba yabba print "Unquoted backwards list: " . @backwards . "\n";; # 3 print "\n"; @backwardsList = reverse @backwards; print "Quoted reversed backwardsList: " . "@backwardsList" . "\n"; # yabba dabba doo print "Unquoted reversed backwardsList: " . @backwardsList . "\n"; # 3 print "\n"; $backwardsScalar = reverse @backwards; print "Quoted reversed backwards scalar: " . "$backwardsScalar" . "\n"; # abbayabbadood print "Unquoted reversed backwardsScalar: " . $backwardsScalar . "\n"; # abbayabbadood PLC - Perl Lists & Arrays (v2.00)

  21. Scalar and List Contexts • What are these contexts? $fred[something] = something; push @fred, something; foreach $something1 (something2) { … } 123 + something; if (something) { … } print something; PLC - Perl Lists & Arrays (v2.00)

  22. 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 PLC - Perl Lists & Arrays (v2.00)

  23. Revision History • Revision History • v1.00, 10/10/2003 5:25 PM, sps Initial revision. -- v2.0, 1/9/2005, chr PLC - Perl Lists & Arrays (v2.00)

More Related