to it ✦ Can use (...) or qw/.../ to define the list ✦ Can initialize an array from another array my @a = (2, 3, 5); my @b = (@a, 10, 20); my @colors = qw/red blue/; my @names = (‘Jim’, ‘Jane’); my @c = (@a, 10, @b); Sunday, November 6, 2011
assignment operator ✦ Copy parts of array using array slices ✦ Slice sizes don’t need to be equal my @a = (2, 3, 5); my @b = (10, 20); my @c = (11, 13, 15); @a = @b; @c[2, 3] = @a[1, 2]; @c[1..10] = (‘t’) x 2; Sunday, November 6, 2011
of the array ✦ pop removes the last element of the array my @a = (2, 3, 5); my @b = (10, 20); my @c = (11, 13, 15); push @a, 10; print pop @a; # prints 10 print pop @a; # prints 5 Sunday, November 6, 2011
of an array ✦ shift removes the first element from the array my @a = (2, 3, 5); my @b = (10, 20); my @c = (11, 13, 15); unshift @a, 10; print shift @a; # prints 10 print shift @a; # prints 2 Sunday, November 6, 2011
✦ splice takes offset and length, deletes the array slice, and changes all subsequent indeces ✦ Careful using in the middle of a for loop Sunday, November 6, 2011
inside parens ✦ Assigning to a list will assign to each of the scalars my @a = (2, 3, 5); my @b = (10, 20); my @c = (11, 13, 15); my ($x, $y) = @a; Sunday, November 6, 2011
list with a single element ✦ Reading a list as a scalar yields the list size ✦ Use scalar function to force scalar context ✦ Use (...) to force list contexts ✦ Some functions will work differently based on the context Sunday, November 6, 2011
= 5; my @b = $x; # now @b is (5) $x = @a; # now $x == 3 print @a; # prints the list print scalar @a; #prints its size if ( @a > 5 ) { print “There are more than 5 elements\n”; } Sunday, November 6, 2011
my $x = f(); Name the context of each colored expression my ($x) = f(); Name the context of each colored expression my @x = f(); Sunday, November 6, 2011
my $x = f(); Name the context of each colored expression my ($x) = f(); Name the context of each colored expression my @x = f(); Scalar List List Sunday, November 6, 2011
print f(); Name the context of each colored expression print int(f()); Name the context of each colored expression print scalar(f()); Sunday, November 6, 2011
print f(); Name the context of each colored expression print int(f()); Name the context of each colored expression print scalar(f()); List Scalar Scalar Sunday, November 6, 2011
push f(), 10; Name the context of each colored expression my $t = f() > 2; Name the context of each colored expression print reverse f(); Sunday, November 6, 2011
push f(), 10; Name the context of each colored expression my $t = f() > 2; Name the context of each colored expression print reverse f(); Error Scalar List Sunday, November 6, 2011
by its index ✦ Note the $. It’s like saying “The third student”, which is singular ✦ Subscripts can be negative my @a = (2, 3, 5); print $a[2]; # prints 5 print $a[0]; # prints 2 print $a[-1]; #prints 5 Sunday, November 6, 2011
and writes the number of words in each line (hint: perldoc -f split) ✦ Write a perl that calculates the 7th element in a fibonacci series (http://en.wikipedia.org/wiki/ Fibonacci_number) Sunday, November 6, 2011
Elements in the even indices are considered keys ✦ Elements in the odd indices are considered values my %shapes = ( triangle => 3, rectangle => 4, ); Sunday, November 6, 2011
✦ Assign to an existing key to overwrite the value ✦ Assign to a non existing key to add a new value my %bus_line = ( 42 => ‘nice’, 24 => ‘fast’, 18 => ‘efficient’, 49 => ‘slow’, ); $bus_line{42} = ‘happy’; $bus_line{3} = ‘clean’; print $bus_line{18}; Sunday, November 6, 2011
the hash ✦ delete removes a value from the hash using a key my %bus_line = ( 42 => ‘nice’, 24 => ‘fast’, 18 => ‘efficient’, 49 => ‘slow’, ); if ( exists $bus_line{42} ) { print “42 is here\n”; } delete $bus_line{18}; Sunday, November 6, 2011
) { print “$line is $desc\n”; } foreach my $line (keys %bus_line) { print “$line is “, $bus_line{$line}; } foreach my $desc (values %bus_line) { print “a bus is $desc\n”; } Sunday, November 6, 2011