380 likes | 500 Views
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.
E N D
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
Accessing Array Elements $fred[0] = “yabba” $fred[1] = “dabba” $fred[2] = “doo”
Accessing Array Elements • You can use an array element in place of a scalar: $copy = $fred[0]; print $fred[0]; $fred[1] +=2;
Array Subscripts • The subscript (index) can be an expression: $fred[2+$number] • Fractions are truncated $num = 2.71828; print $fred[$num - 1];
Array Subscripts • Referencing past the end of array gives “undef”: $fred[0] = “a”; $fred[1] = “b”; print $fred[2];
Special Array Indices • Last array index: $#rocks • Change size of array $#rocks = 2; $#rocks = 99; • Access last element $rocks[$#rocks] = ‘the last rock’;
Negative Indices • The last element $rocks[$#rocks]; $rocks[-1]; • Does not “wrap around”
List Literals (1, 2, 3) (“fred”, 4.5) ()
Ranges (1..5) (1.7..5.7) (5..1) (0, 2..6, 10, 12) ($a..$b) (0..$#rocks)
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.
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 !
List Assignment ($fred, $dino) = (‘flinstone’, undef); ($fred, $barney) = ($barney, $fred); Extras? ($fred, $barney) = qw <flinst, ruble, slate> ($wilma, $dino) = qw[flinstone]
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);
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.
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.
Shift and Unshift @array = qw# fred barney #; $m = shift (@array); $n = shift @array; unshift (@array, 5); unshift (@array, @others);
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’;
More Interpolating @fred = qw (hello dolly); print “my name is $fred[1]”; $y = 2; print “my name is $fred[$y - 1]”;
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]
Perl’s Favourite Default • Perl uses $_ when you forget to use a variable: foreach (0..10) { $_ += 2; print; }
Reverse @fred = (0..10); @barney = reverse (@fred); @wilma = reverse (0..6); @fred = reverse (@fred);
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
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.
Context • When parsing your code perl is expecting either a scalar or an array: 42 + $something sort @something
Context @people = qw (fred barney betty); @sorted = sort @people; $number = 5 + @people; @list = @people; $n = @people;
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/
More Context $fred = something #scalar @pebbles = something #list ($wilma, $betty) = something #list ($dino) = something #list
Scalar Contexts $fred[3] = something 123 + something if (something) { ….} while (something) {…}
List Contexts @fred = something; ($fred, $barney) = something; ($fred) = something; push @fred, something foreach $fred (something) {…} reverse something
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 = ();
<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>);