Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting...

104
Introduction to Perl By Arun Krishnan, Ph.D Projects Leader, HPC Bioinformatics Institute

Transcript of Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting...

Page 1: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Introduction to Perl

ByArun Krishnan, Ph.DProjects Leader, HPCBioinformatics Institute

Page 2: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Introduction to Perl• Perl is a scripting language that makes

manipulation of text, files, and processes easy.

• Perl is a cross between shell programming and the C programming language. C

(numbers) Shell programming

(text)Smalltalk(objects)

C++(numbers, objects)

Perl(text, numbers)

Java(objects)

Page 3: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Introduction to Perl• Perl provides a more concise and readable way to

do many tasks compared to C++ and shell scripts. • Perl has replaced shell programming as the most

popular programming language for text processing and Unix system administration.

• Perl was originally designed under Unix, but now also runs under all operating systems (including Windows).

• Perl is also a popular language for CGI and GUI programming.

Page 4: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Introduction to Perl• The perl command on our system invokes

the latest version Perl 5.0.• You can run the script directly if you make the

script executable, and the first line is of the following form ( #!/usr/... must start from the first column):

$ chmod +x simple$ cat simple#!/usr/local/bin/perl -w$ simple$

Page 5: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Basic Syntax• The -w option tells Perl to produce

extra warning messages about potential dangers. Use -w in all your Perl programs for now.

#!/usr/local/bin/perl -w

• Whitespace doesn't matter in Perl (like C++), except for

#!/usr/local/bin/perl -w

which must start from column 1 on line 1.

Page 6: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Basic Syntax• All perl statements end in a semicolon ;

(like C++)

• In Perl, comments begin with # (like shell scripts)– everything after the # to the end of the line is

ignored. – # need not be at the beginning of the line. – there are no C++-like multiline comments: /* */

Page 7: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Perl Example 1• Here is a “hello world” Perl program:

$ cat hello

#!/usr/local/bin/perl -w# comment lines start with the # characterprint "Hello world\n";$ hello

Hello world$

The print command sends the string to the screen, and “\n“ adds a newline.

Page 8: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Perl Example 1• You can optionally use parenthesis around

the argument in print:

print ("Hello world\n");

or, if your prefer the C++ function style:

print("Hello world\n");

Page 9: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Scalar Variables• A scalar variable can hold a single number or

string (like shell variables), including integers and floating-point numbers (unlike shell variables).

• Scalar variables begin with “$” followed by a letter, and then possibly more letters, digits, or underscores. (e.g., $n, $n1, $name, $first_name).

• Scalar variables are case sensitive.

Page 10: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Assigning Scalar Variables• Scalars are assigned using “=“

$scalar = expression;

• To assign a value to a scalar variable:$number = 25;

$name = "Bill Clinton";

• Unlike shell scripts, use the “$” both when the variable is used and assigned:

$ cat test1

#!/usr/local/bin/perl -w $number = 25;$name = "Bill Clinton";print "$number $name\n";$ test125 Bill Clinton$

Page 11: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Numerical Scalar Variables• Internally, all numerical scalar values are stored

as floats (so you don’t have to worry about integer division in Perl like you do in C++).

• Perl supports the usual C++ numerical operations:

$a = 25; # $a is now 25

$a += 5; # $a is now 30

$a *= 3; # $a is now 90

$a++; # $a is now 91

--$a; # $a is now 90

$result = ($a + 2) * 3.4; # $result is 312.8

Page 12: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

User Input• Use <STDIN> to get input from the user:

$ cat test2

#!/usr/local/bin/perl -w print "Enter name: ";$name = <STDIN>;chomp ($name);print "How many girlfriends do you have? ";$number = <STDIN>;chomp($number);print "$name has $number girlfriends!\n";

$ test2

Enter name: Bill ClintonHow many girlfriends do you have? more than youBill Clinton has more than you girlfriends!

Page 13: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

User Input• <STDIN> grabs one line of input, including the

newline character. So, after:$name = <STDIN>;

if the user typed “Bill Clinton[ENTER]”, $name will contain: “Bill Clinton\n”.

• To delete the newline, the chomp() function takes a scalar variable, and removes the trailing newline if present. (If there is no newline at the end, it does nothing.)

• A shortcut to do both operations in one line is:chomp($name = <STDIN>);

Page 14: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

$• As with the shell scripts, use a backslash before

$ if you really want to print the dollar sign: $ cat test4#!/usr/local/bin/perl -w print "Enter amount: ";$cost = <STDIN>;print "The total is: \$$cost";$ test4Enter amount: 18.50The total is $18.50

• No need to use chomp() if the newline on $costcan be used when it is printed.

Page 15: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Numerical Example$ cat test6

#!/usr/local/bin/perl -wprint "Enter height of rectangle: ";$height = <STDIN>;print "Enter width of rectangle: ";$width = <STDIN>;$area = $height * $width;print "The area of the rectangle is $area\n";$ test6Enter height of rectangle: 10Enter width of rectangle: 5The area of the ractangle is 50$ test6Enter height of rectangle: 10.1Enter width of rectangle: 5.1The area of the rectangle is 51.51

Page 16: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Backquotes:Command Substitution

• You can use command substitution in Perl like in shell scripts:

$ whoami clinton$ cat test7

#!/usr/local/bin/perl -w$user = `whoami`;chomp($user);$num = `who | wc -l`;chomp($num);print "Hi $user! There are $num users logged on.\n";$ test7

Hi clinton! There are 6 users logged on.

• Command substitution will usually include a newline, so use chomp().

Page 17: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Backquote Example 2$ cat big1#!/usr/local/bin/perl -w$dir = `pwd`;chomp($dir);$big = `ls -l | sort +4 | tail -1 | cut -c55-70`;chomp($big);$nline = `wc -l $big | cut -c6-8`;chomp($nline);$nword = `wc -w $big | cut -c6-8 `;chomp($nword);$nchar = `wc -c $big | cut -c6-8 `;chomp($nchar);print "The biggest file in $dir is $big.\n";print "$big has $nline lines, $nword words, $nchar characters.\n";$ big1The biggest file in /home/arun/perl is big1.big1 has 14 lines, 66 words, 381 characters.

Page 18: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Control Flow• Perl has several control flow

statements:–if–while–for–unless–until–do while–do until–foreach

Page 19: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

if• The Perl if statement works almost

the same as in C++:#!/usr/local/bin/perl -w$user = `whoami`;chomp($user);if($user eq "clinton"){

print "Hi Bill!\n";}

• The eq operator compares two strings, and returns true if they are equal (use == for numeric comparisons).

• The curly braces { } are always required in Perl (even if only one statement inside, unlike C++). This avoids the “dangling else” problem.

Page 20: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

if else• The if else statement is similar:

#!/usr/local/bin/perl -w$user = `whoami`;chomp($user);if ($user eq "clinton") {

print "Hi Bill!\n";} else {

print "Hi $user!\n";}

Page 21: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

if elsif else• You can also handle a list of cases:

#!/usr/local/bin/perl -w$users = `who | wc -l`;chomp($users);if ($users > 4){

print "Heavy load!\n";} elsif ($users > 1){

print "Medium load\n";}else {

print "Just me!\n";}

Page 22: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Relational Operators• Perl’s numeric and string comparison

operators:

Comparison Numeric StringEqual == eqNot equal != neLess than < ltGreater than > gtLess than or equal to <= leGreater than or equal to >= ge

Page 23: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Truth in Perl• Truth is flexible in Perl:

– Expressions that evaluate to false0 # traditional false value"" # the null string

"0" # only non-zero length false string

– Some examples of truth:1 # traditional true value684 # non-zero numerical values

are true" " # whitespace is true "hello" # strings are true "00" # a string

Page 24: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

And, Or, Not• 1 represents true, and 0 false (as in C++).• You can also combine and negate

expressions with logical and (&&), logical or (||), and not (!) just like in C++:#!/usr/local/bin/perl -wchomp($user = `whoami`);chomp($nme = `who | grep $user | wc -l`);chomp($nusers = `who | wc -l`);if($nusers - $nme && $user ne "clinton"){

print "Someone else is logged in!\n";} else{

print "All is well!\n";}

Page 25: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

while• The while statement loops indefinitely,

while the condition is true, such as a user-controlled condition:

#!/usr/local/bin/perl -w$resp = "no";while($resp ne "yes"){

print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);

}$ test11Wakeup [yes/no]? noWakeup [yes/no]? yWakeup [yes/no]? yes

$

Page 26: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

for• for can be used as in C++ to do

incrementing loops:$ cat fac

#!/usr/local/bin/perl -wprint "Enter number: ";$n = <STDIN>;$fac = 1;for($i=1; $i<=$n; $i++){

$fac *= $i;}print "The factorial of $n is $fac\n";$ facEnter number: 5The factorial of 5is 120$

Don’t forget to chomp $n

Page 27: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

for• With chomp():

$ cat fac

#!/usr/local/bin/perl -wprint "Enter number: ";chomp($n = <STDIN>);$fac = 1;for($i=1; $i<=$n; $i++){

$fac *= $i;}print "The factorial of $n is $fac\n";

$ fac

Enter number: 5The factorial of 5 is 120

$

Page 28: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

last• The last command works like the C++ break command, breaking out of the innermost loop :$ cat test12

#!/usr/local/bin/perl -wwhile(1){

print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);if($resp eq "yes"){

last;}

}$ test12Wakeup [yes/no]? noWakeup [yes/no]? yWakeup [yes/no]? yes

$

Page 29: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

String Operators• Concatenate strings with the “.” operator (a

period). $ cat string#!/usr/local/bin/perl -w$name = "Bill" . "Clinton";print "$name\n";print "Bill"."Gates"."\n";$ stringBillClintonBillGates$

Page 30: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

String Operators• The string repetition operator x allows you

to repeat a string several times:$ cat string1

#!/usr/local/bin/perl -w$name = "Bill"x3;print "$name\n";$n = 4;print "Bill" x 2 . "Gates" x $n . "\n";print 5;print "\n";$test = ($n+1) x 4;print "$test\n";$ string2BillBillBillBillBillGatesGatesGatesGates55555$

Page 31: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Variable Interpolation• Putting variables inside double quotes is

called variable interpolation. We have seen many examples of this.

• The variable name will be the longest possible variable name that makes sense at that part of the string.

• Enclose the variable in a pair of curly braces if needed to override this.

Page 32: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Variable Interpolation$ cat bill1#!/usr/local/bin/perl -w$bill = "trouble";$billgates = "cheap";print "Bill is $bill\n";print "Bill is $billgates\n";print "Bill is ${bill}gates\n";print "Bill is "."$bill\n";print "Bill is "."$bill"."\n";$ bill1Bill is troubleBill is cheapBill is troublegatesBill is troubleBill is trouble$

Page 33: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Exponentiation• Perl has an exponentiation operator **

unlike C++:$ cat exp#!/usr/local/bin/perl -w$n = 2;$m = 3;$result = $n ** $m;print "$n raised to the $m power is

$result\n";$ exp2 raised to the 3 power is 8$

Page 34: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Operator Precedence• Operator precedence is basically the same

as in C++. • As in C++, you can use parentheses to

override precedence, and to clarify the grouping.

$ cat prec

#!/usr/local/bin/perl -w$n = 2;$m = 3;$result = $n + 1 * $m;print "$n plus one times $m is $result\n";$result = ($n + 1) * $m;print "$n plus one times $m is $result\n";$ prec2 plus one times 3 is 5$ prec

Page 35: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Lists• A list is an ordered collection of scalar data.• A list begins and ends with parentheses, with the

elements separated by commas (and optional spaces).

(1,2, 3,4.1)

• List elements can be constants or expressions: ("Bill", 4, "pie", "B. Gates")($num, 17, $num+1+$i)

• Memory for lists is dynamically allocated and removed as the program runs.

Page 36: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Lists• The empty list (no elements) is represented by

an empty pair of parenthesis:( ) # empty list

• The list constructor “..” creates a list of values with increments of 1:

(1 .. 4) # same as (1, 2, 3, 4)(1..4) # same as (1, 2, 3, 4)(1.2 .. 5.7) # same as (1, 2, 3, 4, 5) ??!!(1, 5 .. 7) # same as (1, 5, 6, 7)($min .. $max) # depends on values of $min and $max(10 .. 5) # same as ( ) -> it can’t count down

Page 37: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Single-Word Lists • There is a shortcut for lists of single-word

strings, the “quote word” function:("bill", "gates", "pie", "toss") # usual versionqw(bill gates pie toss) # same as aboveqw(bill

gates pie toss) # also okay

Page 38: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Arrays• An array contains a list (zero or more scalar

values). • Array variable names are similar to scalar variable

names, except the initial character is “@” instead of “$”.

@numbers = (1,2, 3,4.1);@all = @numbers; # copies array to @all@list1 = ("Bill", 4, "pie", "B. Gates");$num = 2;@group = ($num, 17, $num+1);

• If a scalar value is assigned to an array variable, it becomes a single-element list automatically:

@a = 4; # becomes (4) automatically

Page 39: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Inserting Arrays• You can also insert array elements into lists:

@numbers = (6,7,8);@numbers = (1, 2, @numbers, 10); # (1,2,6,7,8,10)@numbers = (0, @numbers); # (0,1,2,6,7,8,10) @numbers = (@numbers, 99); # (0,1,2,6,7,8,10,99)

• Note that the inserted array elements are at the same level as the other elements, not in a “sub-list”.

Page 40: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Left-Side Assignment• If a list only contains variables, you can use it

on the left side of an assignment:

($a,$b,$c) = (1,2,3); # set $a=1, $b=2, $c=3($a,$b) = ($b,$a); # swap $a and $b($d,@bill) = ($a,$b,$c); # set $d=$a and @bill=($b,$c)($e,@bill) = @bill; # remove first element of @bill

# and put it in $e# end up with: $a=2, $b=1, $c=3# $d=2, $e=1, @bill=(3)

• An array variable can only occur in the last position in the list, because the array variable is “greedy” and consumes all the remaining values.

Page 41: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Array Length• If an array variable is assigned to a scalar variable,

the number assigned is the length of the array:@nums = (1,2,3);$n = @nums; # $n gets 3, the length of @nums

• The context determines whether the length of the array is used or the list:$n = @nums; # $n gets the length of @nums($n) = @nums; # $n gets the first element of @nums

– The first assignment is a scalar assignment, so @nums is treated as a scalar, returning its length.

– The second assignment is an array assignment, and gives the first element of @nums (silently discarding the rest).

Page 42: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Array Subscripting• Each element of an array can be accessed by its

integer position in the list. • The first element starts at position 0 (like C++

arrays).• The first element of the @a array is accessed as

$a[0]:@a = qw(bill gates pie toss);$name1 = $a[0]; # sets name1 to "bill"$name2 = $a[3]; # sets name2 to "toss"$a[1] = "clinton"; # a: qw(bill clinton pie toss)

– Note that the @ on the array name becomes a $ when accessing individual elements.

Page 43: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Array Subscripting• You can use all the usual scalar operations on the

array elements:@a = (1,2,3);$a[0]++; # a: (2,2,3)$a[1] += 4; # a: (2,6,3)$a[2] += $a[0]; # a: (2,6,5)

# swap the first two elements($a[0],$a[1]) = ($a[1],$a[0]); # a: (6,2,5)

Page 44: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Array Slices• Accessing a list of elements from the same array is

called a slice.• Perl provides a special shortcut for slices:

@a = (1,2,3);@a[0,1] = @a[1,0]; # swap the first two elements@a[0,1,2] = @a[1,1,1]; # make all 3 elements like the 2nd@a[1,2] = (7,4); # change the last two to 7 and 4

# a: (1,7,4)

• Note that slices use @ rather than $. This is because slices work with lists rather than scalar values.

Page 45: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

List Slices• Slices also work directly on lists:

$c = (1,2,3,4,5)[2]; # sets $c to 3

@b = (1,2,3,4,5)[2,4]; # sets @b to (3,5)

– The second statement above is equivalent to:

@x = (1,2,3,4,5);

@b = @x[2,4];

Page 46: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Index Expressions• You can use expressions for the subscripts

just like in C++:

@list1 = (5,6,7);

$n = 2;

$m = $list1[$n]; # $m = 7

$p = $list1[$n-1]; # $p = 6

($p) = (5,6,7)[$n-1]; # same thing using slice

Page 47: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Slice Expressions• You can use also array expressions to index

slices if you want to be tricky:

@nums = (5,6,7);

@i = (2,1,0);

@rev = @nums[@i];

# same as @nums[2,1,0]

# or ($nums[2], $nums[1], $nums[0])

# or (7,6,5)

Page 48: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Bad Subscripting• If you access an array beyond the end of the array,

the undef value is returned without warning.• undef is 0 when used as a number, the empty

string when used as a string.@nums = (5,6,7);$nums[3] = "bill"; # @nums: (5,6,7,"bill")$nums[5] = "g."; # @nums: (5,6,7,"bill",undef,"g.")

• Assignment to an array element with a subscript less than zero is a fatal error.

Page 49: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Backward Subscripting• You can use $#bill to get the index value of

the last element of @bill.

• Accessing an array with a negative subscript counts back from the end. So, another way to get the last element of @bill is $bill[-1].

@bill = qw(cheap rich lucky likespie);print $#bill; # prints 3print $bill[$#bill];# prints likespieprint $bill[$#bill-1]; # prints luckyprint $bill[-1]; # prints likespieprint $bill[-2]; # prints luckyprint $bill[-3]; # prints rich

Page 50: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

push and pop• You can use push and pop to add and

remove values from the end of an array.

@a = (1,2,3);$new = 6;push(@a,$new); # same as @a = (@a,$new)

# so far: (1,2,3,6)$oldvalue = pop(@a); # removes last element of @a

# so far: (1,2,3)push(@a,4,5,6); # can push multiple values

# so far: (1,2,3,4,5,6)

• pop returns undef if given an empty array.

Page 51: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

unshift and shift• You can use unshift and shift to add

and remove values from the beginning of an array.

@a = (1,2,3);$new = 6;unshift(@a,$new); # same as @a = ($new, @a)

# so far: (6,1,2,3)$old = shift(@a); # removes first element of @a

# so far: (1,2,3)unshift(@a,4,5,6); # can unshift multiple values

# same as @a = (4,5,6,@a)# so far: (4,5,6,1,2,3)

• shift returns undef if given an empty array.

Page 52: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

reverse and sort• The reverse function reverses the array,

returning the resulting list:@a = (1,2,3);@b = reverse(@a); # @b=(3,2,1), @a unchanged@b = reverse(1,2,3);# same thing@b = reverse(@b); # @b=(1,2,3)

• The sort function returns a sorted array in ascending ASCII order:

@size = qw(small medium large);@sortsize = sort(@x); # large, medium, small@sortsize = sort(qw(small medium large)); # same@a = (1,2,4,8,16,32,64);@b = sort(@a); # @b=(1,16,2,32,4,64,8)

Page 53: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Array Variable Interpolation

• Array elements can be interpolated in double-quoted strings:

@comp111 = qw(unix shell perl);print "$comp111[0] programming is mainly done ";print "in $comp111[2]\n";$n=3;print "$comp111[0] programming is mainly done ";print "in $comp111[$n-1]\n"; # same thing

• Arrays and array slices will be interpolated (printed) with spaces between the elements:

@a = (1,2,"bill",3);print "a: @a\n"; # prints a: 1 2 bill 3print "a1: @a[1,2]\n"; # prints a1: 2 bill

Page 54: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Scalar and List Context• If an operator or function expects a scalar argument,

the argument is evaluated in a scalar context.$n = @nums; # $n gets the length of @nums

• If an operator or function expects a list argument, the argument is evaluated in a list context.

($n) = @nums; # $n gets the first element of @nums

• A scalar value used within a list context is promoted to a single-element array.

@nums = 1; # @nums = (1)

Page 55: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

@ and $ Review

@ (at sign) Refers to the entire array or slice of an array (when used with [ ]).

$ (dollar sign) Refers to one element of the array, used with [ ]

Page 56: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Array chomp

• The chomp function also works for array variables, removing any ending newlines in each element :

@comp111 = qw(unix\n shell\n perl);chomp(@comp111);# @comp111 is now qw(unix shell perl)

• Array chomp is especially useful when reading a file or input from a user.

• <STDIN> in a list context will return all remaining lines up to the end of the file, or until the user hits CTRL-D.

@lines = <STDIN>; # read input in a list contextchomp(@lines); # remove all trailing newlines

Page 57: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Example: Max Value

• How to find the biggest value in an array of numbers @a:

@a = (23,4,56,99,36,24);$max = $a[0];for($i=1; $i<@a; $i++){

if($a[$i] > $max){$max = $a[$i];

}}print "max is: $max\n";

Page 58: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Control Flow• We have already seen several Perl

control flow statements:– if– while– for– last

• Other control flow statements:– unless– until– do while– do until– foreach– next– redo

Page 59: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

unless• The Perl unless statement is like an if

statement with the condition negated:if($temperature <= 20){

print "too cold!\n";}

unless($temperature > 20){ # same thingprint "too cold!\n";

}-------------------------------------------if(!$hot){

print "too cold!\n";}

unless($hot){ # same thingprint "too cold!\n";

}

Page 60: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

unless• unless can have else, just like if:

#!/usr/local/bin/perl -wprint "Enter temperature: ";chomp($temperature = <STDIN>);unless($temperature > 20){

print "too cold!\n";}else{

print "too hot!\n";}

Page 61: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

unless• unless can also have elsif, just like if:

#!/usr/local/bin/perl -wprint "Enter temperature: ";chomp($temperature = <STDIN>);unless($temperature >= 20){

print "too cold!\n";}elsif($temperature == 20){

print "ok!\n";}else{

print "too hot!\n";}

Page 62: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

until• The Perl until statement is like an while

statement with the condition negated.• Sometimes is is easier to say “until something is

true” rather than “while not this is true”:

while(!$endoffile){...

}

until($endoffile){ # same thing...

}

Page 63: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

until• The until statement loops indefinitely,

until the condition is true, such as a user-controlled condition:

#!/usr/local/bin/perl -w$resp = "no";until($resp eq "yes"){

print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);

}$ test11Wakeup [yes/no]? noWakeup [yes/no]? yWakeup [yes/no]? yes$

Page 64: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

do while• The do while statement is like the C++ do while statement.

• It loops indefinitely, while the condition is true, such as a user-controlled condition:

• do while always executes the body of the loop at least once.

#!/usr/local/bin/perl -wdo{

print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);

}while($resp ne "yes");$ test11Wakeup [yes/no]? noWakeup [yes/no]? yes$

Page 65: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

do until• The do until statement loops

indefinitely, until the condition is true, such as a user-controlled condition.

• do until always executes the body of the loop at least once.

#!/usr/local/bin/perl -wdo{

print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);

}until($resp eq "yes");$ test11Wakeup [yes/no]? noWakeup [yes/no]? yWakeup [yes/no]? yes

Page 66: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

foreach• foreach takes a list of values and assigns them

one by one to a scalar variable.• The body of the loops is executed once for each

successive assignment.• foreach is similar to the shell programming’s for statement.

foreach $i (@some_list){...

}

Page 67: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

foreach• The following example sums the contents

of an array:

$ cat sum

#!/usr/local/bin/perl -w@a = (21,32,3,44,75,16,19);$sum = 0;foreach $b (@a){

$sum += $b;}print "The array sum is: $sum\n";$ sumThe array sum is: 210$

Page 68: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

foreach• foreach allows us to easily print an array

in our own customized way.• The following example prints an array with

each element separated by 2 spaces:

$ cat print1

#!/usr/local/bin/perl -w@a = (1,2,3,4,5);foreach $i (@a){

print "$i ";}print "\n";$ print11 2 3 4 5$

Page 69: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

foreach• The following example prints the numbers in

reverse order without changing the array:

$ cat print2

#!/usr/local/bin/perl -w@a = (1,2,3,4,5);foreach $i (reverse @a){

print "$i ";}print "\n";$ print25 4 3 2 1 $

• reverse @a is the same as writing reverse(@a). Parenthesis are always optional on Perl functions.

Page 70: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

foreach $_• If you omit the scalar variable in foreach, Perl

will use $_ automatically:

$ cat print3

#!/usr/local/bin/perl -w@a = (1,2,3,4,5);foreach (reverse @a){

print;}print "\n";$ print354321 $

• print (and other Perl functions) use $_ as the default if nothing is specified.

Page 71: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

foreach $_• Of course, if you want double spaces, you will

have to use $_ explicitly:

$ cat print3a

#!/usr/local/bin/perl -w@a = (1,2,3,4,5);foreach (reverse @a){

print "$_ ";}print "\n";$ print3a5 4 3 2 1 $

Page 72: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

foreach• The scalar variable in foreach is an alias for

each variable in the list, not just a copy. (Tricky!)• If you modify the scalar variable in foreach, the

aliased element in the list is also changed:$ cat double

#!/usr/local/bin/perl -w@a = (1,2,3,4);foreach $b (@a){

$b *= 2;}print "@a\n";$ double2 4 6 8 $

Page 73: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

last Again• The last command only breaks out of while, until, for, and foreach loops.

• For mysterious reasons, do while and do until do not count as loops in Perl.

$ cat last1

#!/usr/local/bin/perl -wdo{

print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);if($resp eq "yes"){

last;}

}while(1);$ last1Wakeup [yes/no]? yesCan't "last" outside a block at last1 line 6, <STDIN> chunk 1.$

Page 74: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

last Again• In the example below, the last statement

causes the outer while loop to be broken:$ cat last2

#!/usr/local/bin/perl -wwhile(1){

do{print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);if($resp eq "yes"){

last;}

}while(1);}$ last2Wakeup [yes/no]? yes$

Page 75: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

next• The next command causes a jump to the

end of the loop, but without terminating the loop.

$ cat next1#!/usr/local/bin/perl -w$resp = "no";until($resp eq "quit" || $resp eq "yes"){

print "Wakeup [yes/no/quit]? ";chomp($resp = <STDIN>);if($resp eq "yes"){

next;}print "sleeping...\n";

}$ next1Wakeup [yes/no/quit]? nosleeping...Wakeup [yes/no/quit]? yes$ next1Wakeup [yes/no/quit]? quitsleeping...

Page 76: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

redo• The redo command causes a jump back

to the beginning of the loop, but without re-testing the condition (next re-tests the condition).

$ cat redo#!/usr/local/bin/perl -w$resp = "no";until($resp eq "quit" || $resp eq "yes"){

print "Wakeup [yes/no/quit]? ";chomp($resp = <STDIN>);if($resp eq "yes"){

redo;}print "sleeping...\n";

}$ redoWakeup [yes/no/quit]? nosleeping...Wakeup [yes/no/quit]? yesWakeup [yes/no/quit]? quitsleeping

Page 77: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

next and redo• Like last, next and redo only break out

of while, until, for, and foreach loops (not do while and do until loops).

$ cat redo1#!/usr/local/bin/perl -w$resp = "no";do{

print "Wakeup [yes/no/quit]? ";chomp($resp = <STDIN>);if($resp eq "yes"){

redo;}print "sleeping...\n";

}until($resp eq "quit" || $resp eq "yes");$ redo1Wakeup [yes/no/quit]? nosleeping...Wakeup [yes/no/quit]? yesCan't "redo" outside a block at redo1 line 7, <STDIN> chunk 2.$

Page 78: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

redo and last/next• You can make a while loop using redo and last/next in a “naked” block (a “naked” block is a block { } that is not part of a loop or if statement):

$ cat redo2#!/usr/local/bin/perl -w{

print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);if($resp eq "yes"){

last; # could also use "next;"}redo;

}$ redo2Wakeup [yes/no]? noWakeup [yes/no]? yWakeup [yes/no]? yes$

Page 79: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Labeled Blocks• What if you want to jump out of two nested loops?• In C++, you’d be in trouble. • In Perl, you can use labeled loops to break out of an

outer loop using last, next, and redo.• For clarity (and other reasons), it is best to choose

label names that are all upper case letters and digits.• Labels always end in a colon “:”.

OUTERLOOP: for($i=1; $i<3; $i++){INNERLOOP: for($j=1; $j<5; $j++){

...

• Add the label (without the colon) as a parameter to last, for example, if you want to exit OUTERLOOP.

last OUTERLOOP;

Page 80: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Labeled Blocks Example$ cat label#!/usr/local/bin/perl -w$sum = 0;LOOP1: for($i1=1; $i1<=2; $i1++){

LOOP2: for($i2=1; $i2<=2; $i2++){LOOP3: for($i3=1; $i3<=2; $i3++){

$sum += 1;print "sum so far: $sum\n";if($sum > 2){

last LOOP2; }

}$sum += 2;

}$sum += 3;

}print "sum at end: $sum\n";$ labelsum so far: 1sum so far: 2sum so far: 5sum so far: 9sum at end: 12$

Page 81: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Backward if• A simple way to write “if this, then that” is:

chomp($user = `whoami`);print("Hi Bill!\n") if($user eq "gates");

is the same as:

chomp($user = `whoami`);if($user eq "gates"){

print "Hi Bill!\n";}

• Backward if avoids having to write the curly braces { }.

• There can only be one statement inside the block.

Page 82: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Backward if• Backward if is a natural and tidy way to exit from

a loop:$ cat backif#!/usr/local/bin/perl -wwhile(1){

print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);last if $resp eq "yes";

};$ backifWakeup [yes/no]? noWakeup [yes/no]? yWakeup [yes/no]? yes$

Page 83: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Backwardunless, while, until

• You can also use backward unless, while, and until (if there is only one statement in the block):

$ cat sum#!/usr/local/bin/perl -wprint "Enter numbers to sum (0 to quit): \n";$sum = 0;$n = 1;$sum += $n = <STDIN> while($n != 0);print "The sum is: $sum: \n";$ sumEnter numbers to sum (0 to quit): 110The sum is: 2$

Page 84: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

&& if• Another simple way to write “if this, then

that” is:

chomp($user = `whoami`);$user eq "gates" && print("Hi Bill!\n");

is the same as:

chomp($user = `whoami`);if($user eq "gates"){

print("Hi Bill!\n");}

Page 85: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

&& ifthis && that;

• Why does this work? • Isn’t && the logical-and operator?• Consider what happens when this and that take on values of true and false:– If this is true, then the value of the entire expression

is still not known, because it depends on the value of that. So that has to be evaluated.

– If this is false, there is no need to look at that, because the value of the whole expression must be false. Since there is no need to evaluate that, Perl skips it.

Page 86: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

&& if• && if is also a tidy way to exit from a loop:

$ cat backif1#!/usr/local/bin/perl -wwhile(1){

print "Wakeup [yes/no]? ";chomp($resp = <STDIN>);$resp eq "yes" && last;

};$ backif1Wakeup [yes/no]? noWakeup [yes/no]? yWakeup [yes/no]? yes$

Page 87: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

|| unless• Similarly, another simple way to write an unless statement is:

$temperature > 20 || print "too cold!\n";

is the same as:

unless($temperature > 20){ # same thing

print "too cold!\n";}

Page 88: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

What is a Hash?• A hash (or associative array) is like an array, where

the index can be any scalar value (not just small non-negative integers).

• A hash index is called a key (keys must be unique). • The elements of a hash have no fixed order, unlike

arrays. • The elements of a hash are like filing cards, where

the top half of the card has the key, and the bottom half has the value.

• The keys are used to lookup the values. keys:

values:

“bill”

“cheap”

16

2046.1

“0 C”

“32 F”

Page 89: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Hash Variables• Hash variable names begin with the percent sign

(%) followed by the usual variable name. • There is no relationship between $bill, @bill,

and %bill, Perl considers them to be separate variables.

• Each element of a hash is a separate scalar variable, accessed by the key.

• Elements of the hash %bill, are referenced with $bill{$key}, where $key is any scalar expression.

Page 90: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Hash Variables• As with arrays, you create new hash

elements:$bill{"Gates"} = "cheap";$bill{"Clinton"} = "busy";$bill{234.5} = 456.7;

• Once created, you can access hash values similar to indexing arrays:

print "Bill Gates is ", $bill{"Gates"}, "\n"; $n = "Clinton";print "Bill $n is $bill{$n}\n";$n = 234.5;print "Bill $n is $bill{$n}\n";

• Output:Bill Gates is cheap Bill Clinton is busyBill 234.5 is 456.7

Page 91: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Hash Variables• Once created, you can change hash values

if needed:

$bill{234.5} = 456.7;...$bill{234.5} += 3; # makes it 459.7

• Referencing a hash element that does not exist returns the undef value.

Page 92: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

List Representation of a Hash

• You can access the hash as a whole if you need to initialize it or to copy it.

• The hash unwinds as a list. Each pair in the list represents the key and its value.

$bill{"Gates"} = "cheap";$bill{"Clinton"} = "busy";$bill{234.5} = 456.7;@billarray = %bill; # @billarray: qw(Gates cheap Clinton busy 234.5 456.7)%a = @billarray; # create %a like %bill%a = %bill; # faster way to do the same%b = qw(Gates cheap Clinton busy 234.5 456.7);# initialize %b like %bill from list values

• The order of the key-value pairs is random in the list representation and cannot be controlled. (Don’t rely on the ordering.)

Page 93: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Hash reverse• You can construct a hash with keys and values

swapped using the reverse function:%aback = reverse %a;

• If %a has two identical values, those will end up as a single element in %aback (reverse is best used on hashes with unique keys and values).

$ cat reverse1#!/usr/local/bin/perl -w$b{"Gates"} = "Bill";$b{"Clinton"} = "Bill";%revb = reverse %b;# print out revb key and value... $ reverse1Bill Gates$

Page 94: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

keys• The keys(%hashname) function returns a list of

all the keys currently in the hash.

$bill{"Gates"} = "cheap";$bill{"Clinton"} = "busy";$bill{234.5} = 456.7;@list = keys(%bill); # @list gets qw(Gates Clinton 234.5) in some random order

• If there are no elements in the hash, then keys()returns an empty list.

• As with other Perl functions, the parentheses are optional:

@list = keys %bill;

Page 95: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

keys• The keys function is often used in foreach loops to print or access the elements one-by-one:

$ cat key

#!/usr/local/bin/perl -w$bill{"Gates"} = "cheap";$bill{"Clinton"} = "busy";$bill{234.5} = 456.7;foreach $k (keys(%bill)){ # for each key of

%billprint "At $k we have $bill{$k}\n";

}$ keyAt 234.5 we have 456.7At Gates we have cheapAt Clinton we have busy$

Page 96: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Printing Hashes• You cannot print the entire hash like you

can arrays:$ cat prhash

#!/usr/local/bin/perl -w$bill{"Gates"} = "cheap";$bill{"Clinton"} = "busy";$bill{234.5} = 456.7;print "Bill hash: %bill\n";$ prhashBill hash: %bill$

• Most Perl programmers use a foreachloop to print hashes (as in the previous slide).

Page 97: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

keys• In a scalar context, keys returns the

number of elements in the hash:$ cat key1#!/usr/local/bin/perl -w$n = keys(%bill);if($n==0){

print "Bill is empty\n";exit;

}print "Bill has $n elements\n";$ key1Bill is empty$

• exit is like the C++ exit() function, and ends the program immediately.

Page 98: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

values• The values(%hashname) function returns

a list of all the values currently in the hash.• The values are in exactly the same order as

the keys returned by keys(%hashname).$ cat value#!/usr/local/bin/perl -w$bill{"Gates"} = "cheap";$bill{"Clinton"} = "busy";$bill{234.5} = 456.7;@klist = keys(%bill); @vlist = values(%bill); print "@klist\n"; print "@vlist\n"; $ value234.5 Gates Clinton456.7 cheap busy$

Page 99: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

each• Another way to print a hash is with the each

function.• each returns a key-value pair as a two-element

list.• Each time each is called it returns the next key-

value pair until all the elements have been accessed.

• When no more pairs, each returns an empty list.

$ cat each#!/usr/local/bin/perl -w$bill{"Gates"} = "cheap";$bill{"Clinton"} = "busy";$bill{234.5} = 456.7;while(($k,$v) = each(%bill)){

Page 100: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

delete• How to remove hash elements?

Answer: Use the delete function.• The argument is the keyed reference to be

deleted:$ cat delete1#!/usr/local/bin/perl -w$bill{"Gates"} = "cheap";$bill{"Clinton"} = "busy";$bill{234.5} = 456.7;delete $bill{"Gates"}; while(($k,$v) = each(%bill)){

print "At $k we have $v\n";}$ delete1At 234.5 we have 456.7At Clinton we have busy$

Page 101: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Hash Slices• Like an array, a hash can be sliced to access a

collection of elements.• We can use a hash slice to compact initialization.

For example:$b{"Gates"} = "cheap";$b{"Clinton"} = "busy";$b{234} = 45;

can be shortened to:($b{"Gates"},$b{"Clinton"},$b{234}) = qw(cheap busy 45);

can be hash sliced as:@b{"Gates","Clinton",234} = qw(cheap busy 45);

(Note that it is @b, not %b.)• Another example:

@b{"A" .. "Z"} = (1 .. 26);

Page 102: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Hash Slices• We can also use a hash slice with variable

interpolation :$ cat hslice#!/usr/local/bin/perl -w@b{"Gates","Clinton",234} = qw(cheap busy 45);@k = qw(Gates Clinton);print "The values are: @b{@k}\n";$ hsliceThe values are: cheap busy$

Page 103: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Hash Slices - Merging Hashes

• Hash slices can also be used to merge hashes.

• In the following example, if there are duplicate keys, the values from the %smallhash are used:

$ cat merge#!/usr/local/bin/perl -w@big{"Gates","Clinton",234} = qw(cheap busy 45);@small{"Horner",234} = qw(111 67);@big{keys %small} = values %small;while(($k,$v) = each(%big)){

print "At $k we have $v\n";}$ mergeAt 234 we have 67At Gates we have cheapAt Horner we have 111

li h b

Page 104: Introduction to Perl - Bioinformatics Institute · Introduction to Perl • Perl is a scripting language that makes manipulation of text, files, and processes easy. • Perl is a

Hash Slices - Merging Hashes

• A simpler (though slower) way to merge hashes is:

$ cat merge1#!/usr/local/bin/perl -w@big{"Gates","Clinton",234} = qw(cheap busy 45);@small{"Horner",234} = qw(111 67);%big = (%big, %small);while(($k,$v) = each(%big)){

print "At $k we have $v\n";}$ merge1At 234 we have 67At Gates we have cheapAt Horner we have 111At Clinton we have busy$