1 / 38

Lists and Arrays

Lists and Arrays. Lists and Arrays. A list is an ordered collection of scalars An array is a variable that contains a list. Lists and Arrays. Each element is a separate scalar value Indexed by small integers Can hold any type of data Smallest has no elements, largest fills available memory.

ganesa
Download Presentation

Lists and 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. Lists and Arrays

  2. Lists and Arrays • A list is an ordered collection of scalars • An array is a variable that contains a list

  3. Lists and Arrays • Each element is a separate scalar value • Indexed by small integers • Can hold any type of data • Smallest has no elements, largest fills available memory

  4. Accessing Array Elements $fred[0] = “yabba” $fred[1] = “dabba” $fred[2] = “doo”

  5. Accessing Array Elements • You can use an array element in place of a scalar: $copy = $fred[0]; print $fred[0]; $fred[1] +=2;

  6. Array Subscripts • The subscript (index) can be an expression: $fred[2+$number] • Fractions are truncated $num = 2.71828; print $fred[$num - 1];

  7. Array Subscripts • Referencing past the end of array gives “undef”: $fred[0] = “a”; $fred[1] = “b”; print $fred[2];

  8. Special Array Indices • Last array index: $#rocks • Change size of array $#rocks = 2; $#rocks = 99; • Access last element $rocks[$#rocks] = ‘the last rock’;

  9. Negative Indices • The last element $rocks[$#rocks]; $rocks[-1]; • Does not “wrap around”

  10. List Literals (1, 2, 3) (“fred”, 4.5) ()

  11. Ranges (1..5) (1.7..5.7) (5..1) (0, 2..6, 10, 12) ($a..$b) (0..$#rocks)

  12. qw Shortcut • Lists of simple words are often needed in perl: $mylist = (‘fred’, ‘barney’, ‘wilma’); $mylist = qw / fred barney wilma /; • Produces a single quoted list - no \n or $fred inside your string.

  13. More About qw • You can use any punctuation symbols: qw / fred barney wilma / qw # http://www.google.com # qw ( /etc/passwd /usr/bin/perl ) qw < fred barney wilma > qw ! Yahoo\! google exite !

  14. List Assignment ($fred, $dino) = (‘flinstone’, undef); ($fred, $barney) = ($barney, $fred); Extras? ($fred, $barney) = qw <flinst, ruble, slate> ($wilma, $dino) = qw[flinstone]

  15. Referring To Arrays • Build an array of strings: ($m[0], $m[1], $m[2]) = qw / gold silver platinum /; OR @rocks = qw /gold silver platinum/; @tiny = (); @giant = (1..1e5);

  16. More Arrays @stuff = (@giant, undef, @giant); @copy = @quarry; $dino = “granite”; @quarry = (@rocks, $dino, @tiny); • An array value that has not been assigned is an empty list.

  17. Push and Pop • A stack is a first in, last out arrangement - like a stack of plates. @array = (5..9); $fred = pop (@array); $fred = pop (@array); pop @array; • If the array is empty, pop does nothing.

  18. Shift and Unshift @array = qw# fred barney #; $m = shift (@array); $n = shift @array; unshift (@array, 5); unshift (@array, @others);

  19. Arrays in Strings

  20. Arrays in Strings • What happens if you use an email address in a string? $email = “fred@bedrock.edu”; $email = “fred\@bedrock.edu”; $email = ‘fred@bedrock.edu’;

  21. More Interpolating @fred = qw (hello dolly); print “my name is $fred[1]”; $y = 2; print “my name is $fred[$y - 1]”;

  22. Fun with [ $fred = “right”; @fred = qw / this is wrong /; print “This is $fred[2]” #wrong print “This is ${fred}[2]”; #right[2] print “This is $fred\[2]”; #right[2] print “This is $fred” . “[2]”; #right[2]

  23. Foreach

  24. Foreach continued

  25. Perl’s Favourite Default • Perl uses $_ when you forget to use a variable: foreach (0..10) { $_ += 2; print; }

  26. Reverse @fred = (0..10); @barney = reverse (@fred); @wilma = reverse (0..6); @fred = reverse (@fred);

  27. Sort • Sorts in ASCII order: • Caps before lowercase • Numbers before letters @metals = qw /gold platinum sliver/ @sorted = sort (@metals); @back = reverse sort @metals; @metals = sort @metals @numbers = sort 97..102

  28. Context

  29. Context • In english, the meaning of a word can vary depend on the context in which is is used: • “I will read the book” • “I have read the book” • Sometimes the value of a perl expression will depend on the context in which you use it.

  30. Context • When parsing your code perl is expecting either a scalar or an array: 42 + $something sort @something

  31. Context @people = qw (fred barney betty); @sorted = sort @people; $number = 5 + @people; @list = @people; $n = @people;

  32. List Producing Expressions in a Scalar Context • When a scalar context is used where list is expected, the results vary depending on the function: $n = sort (@metals); $fred = reverse qw /yabba dabba doo/

  33. More Context $fred = something #scalar @pebbles = something #list ($wilma, $betty) = something #list ($dino) = something #list

  34. Scalar Contexts $fred[3] = something 123 + something if (something) { ….} while (something) {…}

  35. List Contexts @fred = something; ($fred, $barney) = something; ($fred) = something; push @fred, something foreach $fred (something) {…} reverse something

  36. Scalar Producing Expressions in List Context • A scalar used in list context is “promoted” to a one element list: @fred = 6*7; print $fred[0]; • To empty an array: @wilma = undef; #wrong! @betty = ();

  37. Forcing Scalar Context

  38. <STDIN> In List Context • In a scalar context, <STDIN> returns one line at a time • In a list context, it reads in until end of file, with one line per element: $oneline = <STDIN>; @alllines = <STDIN>; chomp (@lines = <STDIN>);

More Related