View
232
Download
3
Embed Size (px)
Citation preview
Lecture 2
BNFO 135
Usman Roshan
Perl variables
• Scalar– Number– String
• Examples– $myname = “Roshan”;– $year = 2006;
Number operators
Strings
• Perl supports very powerful operations on strings. Basic operators below.
Arrays
• Array variables begin with @.
• Example– @myarray=(1,2,3,4,5);– @s=(“dna”, “rna”, “protein”);– print @s outputs dnarnaprotein
Array operations
Hashes
• Hashes are like arrays, except that they are indexed by user defined keys instead of non-negative integers.
• Hashes begin with %• Examples
– %h=(‘red’, 1, ‘blue’, 2, ‘green’, 3);– %h=(“first”, “usman”, “last”, “name”);
Perl online references
• http://www.rexswain.com/perl5.html• http://www.perl.com/pub/q/documentation• http://www-cgi.cs.cmu.edu/cgi-bin/perl-man• http://www.squirrel.nl/pub/perlref-5.004.1.pdf• http://perldoc.perl.org/index-language.html
Input and Output
• printf: C-like formatted output, very powerful
• print: unformatted output
• Example:– printf “My name is %s\n”, $name;– printf “Today is %s %d %d\n”, $month,
$day, $year;
File I/O
open(IN, “in.txt”);
$line=<IN>;
print $line;
close IN;
open(OUT, “>out.txt”);
print OUT “line1\n”;
close OUT;
File I/O
open(OUT, “>>out.txt”);print OUT “line2\n”;close OUT;
open(IN, “in.txt”);@lines=<IN>; #reads all lines into arrayprint @lines;close IN;
$input=<STDIN>; #read from standard input
Control structures---if else block
• if (c)
• { s1; s2 ;...;}
• if (c)
• { s1; s2;...;}
• else
• { s1; s2;...;}
• if (c1) • { s1; s2; ...;} • elsif (c2) • { ... } • elsif (c3) • {....} • else • { ...};
Control structures---if else block
This is program to compute max of two
Numbers.
• $max=0;
• if($x > $y){ $max = $x;}
• else {$max = $y;}
Control structures---for loop
• for(init_exp; test_exp; iterate_exp)– {s1; s2; … ;}
• foreach $i (@some_list) { s1; s2; ...; }
• Examples:• for ($i=0; $i < @arr; $i++)
– { print $arr[$i]; }
• foreach $e (@arr) – { print $e; }
Control structures---while loop
• while (condition) { s1; s2;...; } • do { s1; s2; ...;} while condition;
• while (condition) {s1;if(c){last;} s2;...; } • #last means exit the loop
• while (condition) {s1;if(c){next;} s2;...; }• #next means go to next iteration
• Write a program to prompt the user for a Yes or No response. Read in the user response using the STDIN file handle and print “OK” is the user enters “Yes,” “I hear you” if the user enters “No,” and “Make up your mind!” if the user enters something other than “Yes” or “No.”
• Create a script called foods.pl that asks the user for their favorite foods. Ask the user to enter at least 5 foods, each separated by a space (or some other delimiter). Store their answer in a scalar. Split the scalar into an array. Once the array is created, have the script do the following:
• * Print the array• * Print the number of elements in the array• * Create an array slice from three elements of the array and print the new array
• Write a program to open a file containing SSNs and read in the first five records into an array.
• Write a program called count.pl that reads in a set of strings from a file called string.txt and prints the number of A’s, B’s, C’s, and D’s. For example, if the file looks like
AABCAACDAADCCC• then your program should outputA 6B 1C 5D 2
Subroutines
• sub <subroutine_name> {– #parameters are placed in @_– <code>– .– .– return;
• }
Scope
• You can create local variables using my.
• Otherwise all variables are assumed global, i.e. they can be accessed and modified by any part of the code.
• Local variables are only accessible in the scope of the code.
Pass by value and pass by reference
• All variables are passed by value to subroutines. This means the original variable lying outside the subroutine scope does not get changed.
• You can pass arrays by reference using \. This is the same as passing the memory location of the array.
Splitting and joining strings
• split: splits a string by regular expression and returns array– @s = split(/,/);– @s = split(/\s+/);
• join: joins elements of array and returns a string (opposite of split)– $seq=join(“”, @pieces);– $seq=join(“X”, @pieces);
Searching and substitution
• $x =~ /$y/ ---- true if expression $y found in $x
• $x =~ /ATG/ --- true if open reading frame ATG found in $x
• $x !~ /GC/ --- true if GC not found in $x• $x =~ s/T/U/g --- replace all T’s with U’s• $x =~ s/g/G/g --- convert all lower case
g to upper case G
DNA regular expressions
Taken from Jagota’s Perl for Bioinformatics