Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;"— Presentation transcript:

1 Lecture 2 BNFO 135 Usman Roshan

2 Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;

3 Number operators

4 Strings Perl supports very powerful operations on strings. Basic operators below.

5 Arrays Array variables begin with @. Example –@myarray=(1,2,3,4,5); –@s=(“dna”, “rna”, “protein”); –print @s outputs dnarnaprotein

6 Array operations

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

8 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

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

10 File I/O open(IN, “in.txt”); $line= ; print $line; close IN; open(OUT, “>out.txt”); print OUT “line1\n”; close OUT;

11 File I/O open(OUT, “>>out.txt”); print OUT “line2\n”; close OUT; open(IN, “in.txt”); @lines= ; #reads all lines into array print @lines; close IN; $input= ; #read from standard input

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

13 Control structures--- if else block This is program to compute max of two Numbers. $max=0; if($x > $y){ $max = $x;} else {$max = $y;}

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

15 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

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

17 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 AABCAA CDAAD CCC then your program should output A6 B1 C5 D2

18 Subroutines sub { –#parameters are placed in @_ – –. –return; }

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

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

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

22 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

23 DNA regular expressions Taken from Jagota’s Perl for Bioinformatics


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

Similar presentations


Ads by Google