23
Lecture 2 BNFO 135 Usman Roshan

Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

  • View
    232

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Lecture 2

BNFO 135

Usman Roshan

Page 2: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Perl variables

• Scalar– Number– String

• Examples– $myname = “Roshan”;– $year = 2006;

Page 3: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Number operators

Page 4: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Strings

• Perl supports very powerful operations on strings. Basic operators below.

Page 5: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Arrays

• Array variables begin with @.

• Example– @myarray=(1,2,3,4,5);– @s=(“dna”, “rna”, “protein”);– print @s outputs dnarnaprotein

Page 6: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Array operations

Page 7: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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”);

Page 8: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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

Page 9: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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;

Page 10: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

File I/O

open(IN, “in.txt”);

$line=<IN>;

print $line;

close IN;

open(OUT, “>out.txt”);

print OUT “line1\n”;

close OUT;

Page 11: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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

Page 12: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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 • { ...};

Page 13: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Control structures---if else block

This is program to compute max of two

Numbers.

• $max=0;

• if($x > $y){ $max = $x;}

• else {$max = $y;}

Page 14: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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; }

Page 15: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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

Page 16: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

• 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.

Page 17: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

• 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

Page 18: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

Subroutines

• sub <subroutine_name> {– #parameters are placed in @_– <code>– .– .– return;

• }

Page 19: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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.

Page 20: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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.

Page 21: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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);

Page 22: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

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

Page 23: Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

DNA regular expressions

Taken from Jagota’s Perl for Bioinformatics