Chapters 3 and 5 in Learning Perl.
(3.14, 'abA', 65065)If
$pi = 3.14 and $s
= 'abA' we can also write
($pi, $s, 65065)We can also do integer ranges:
(-1..5)shorthand for
(-1, 0, 1, 2, 3, 4, 5)Counting down not allowed!
my $x = 65065; my @x = ($pi, 'abA', $x); my @y = (-1..5); my @z = ($x, $pi, @x, @y); my ($first, @rest) = @z;
$z[0] # 65065 $z[0] = 2; $z[0] # 2 $z[$#z]; # 5Skip "slices" for now.
@z;
push @z, 9;Take the 9 off the end of
@z, and
then take the 5 off the end:
my $end1 = pop @z; my $end2 = pop @z;Add 9 to the beginning of
@z;
unshift @z, 9;Take the 9 off the beginning of
@z, and
then take the 3.14 off the beginning:
my $b1 = shift @z; my $b2 = shift @z;
my @zr = reverse @z;
my @zs = sort @z;Numerically:
my @q = sort { $a <=> $b } (-1, 3, -20)
my @q = split /\d+/, 'abd1234deff0exx'
# ('abd', 'deff', 'exx');
my @i = <>; chomp @i;
reverse, and can be used to
get the size of an array.
print reverse 'ab'; # prints ab!!! (reverse in array context) $ba = reverse 'ab'; # $ba contains 'ba' (reverse in scalar context) print scalar reverse 'ab'; # prints ba print scalar @z; # print the size of @z
my %translate;
$translate{'atg'} = 'M';
$translate{'taa'} = '*';
$translate{'ctt'} = 'K'; # oops
$translate{'ctt'} = 'L'; # fixed
print $translate{'atg'};
keys %translate
delete $translate{'taa'};
keys %translate;
%translate = ( 'atg' => 'M',
'taa' => '*',
'ctt' => 'L',
'cct' => 'P', );
How the program (call it mapper.pl
in exercise 1 works:
$ mapper.pl red apple $ mapper.pl green leaves $ mapper.pl blue ocean
Lincoln Stein lstein@cshl.org Last modified: Mon Oct 18 14:50:33 EDT 2004