140 likes | 227 Views
More loops. Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.<br>"; while ($num != 31) { $num = <STDIN>; } print "correct!<br>";. Loops. my @names = <STDIN>; chomp(@names); my $name; foreach $name (@names) { print "Hello $name!<br>"; }.
E N D
Commands inside a loop are executed repeatedly (iteratively): my $num=0;print "Guess a number.\n";while ($num != 31) { $num = <STDIN>;}print "correct!\n"; Loops my @names = <STDIN>;chomp(@names);my $name;foreach $name (@names) { print "Hello $name!\n";}
The for loop is controlled by three statements: • 1st is executed before the first iteration • 2nd is the stop condition • 3rd is executed before every re-iteration Loops: for my $i=0;while ($i<10){ print "$i\n";$i++;} for (my $i=0; $i<10; $i++) { print "$i\n";} These are equivalent
Breaking out of loops next– skip to the next iteration last– skip out of the loop my @lines = <STDIN>;my $line;foreach $line (@lines) { if (substr($line,0,1) eq ">") { next; } if (substr($line,0,8) eq "**stop**") { last; } print $line;}
Breaking out of loops die– end the program and print an error message to the standard error <STDERR> if ($score < 0) { die "score must be positive"; }score must be positive at test.pl line 8. Note: if you end the string with a "\n" then only your message will be printed * warn does the same thing as die without ending the program
If you declare a variable inside a loop it will only exist in that loop: my $name; while ($name ne "Yossi") { chomp($name = <STDIN>); print "Hello $name, what is your age?\n"; my $age; $age = <STDIN>; }print $name; print $age; Global symbol "$age" requires explicit package name Scope of variable declaration
If you declare a variable name twice, outside and inside – you are creating two distinct variables… don’t do it! my $name = "Ruti"; print "Hello $name!\n";my $number;foreach $number (1,2,3) { my $name = "Nimrod"; print "Hello $name!\n";}print "Hello $name!\n"; Never declare the same variable name twice Hello Ruti!Hello Nimrod!Hello Nimrod!Hello Nimrod!Hello Ruti!
We encourage using meaningful names (even if they are long). • For multi-word variables use no spaces, and each word, except for the first, is capitalized. examples: • $proteinHeader • $studentNameArray • We will see conventions for other kinds of variables and names as we continue. Naming variables
Start Read sequence Find most downstream CTAG Get the 10bp tag Print the tag End of input? No End FASTA: Analyzing complex input >gi|24646380|ref|NM_079608.2| Mus musculus EH-domain containing 4 (EHD4), mRNA GTGGTATTTCTTCGTTGTCTCTGGCGTGGTCACGTTGATTGGTCCGCTATCTGGACCGAAAAAAGTCGTA......GTCGACGGCGATGGGTTCCTGGACTCTGACGAGTTCGCGCTGGCCTTGCACTTAATCAACGTCAAGCTGGAAGGCTGCGAGCTGCCCACCGTGCTGCCGGAGCACTTAGTACCGCCGTCGAAGCGCTATGACTAGTGTCCTGTAGCATACGCATACGCACACTAGATCACACAGCCTCACAATTCCCAAAAAAAAAAAAAAAA >gi|71895640|ref|NM_001031040.1| Mus musculus EH-domain containing 3 (EHD3), mRNAGGTAGGGCGCTACCGCCTCCGCCCGCCTCTCGCGCTGTTCCTCCGCGGTATGCCCGCGCCGGCAGCCGGC......TATTATATAGAGAAATATATTGTGTATGTAGGATGTGCTTATTGCATTACATTTATCACTTGTCTTAACTAGAATGCATTAACCTTTTTTGTACCCTGGTCCTAAAACATTATTAAAAAGAAAGGCTAAAAAAAAAAAAAAAAA >gi|55742710|ref|NM_153068.2| Mus musculus EH-domain containing 2 (Ehd2), mRNATGAGGGGGCCTGGGGCCCGCCCTGCTCGCCGCTCCTAGCGCACGCGGCCCCACCCGTCTCACTCCACTGC......
Start Read line Save header Read line Concatenate to sequence Read line Header? No Yes Do something End of input? No End • Overall design: • Read the sequence • Do something • Let’s see how it’s done… FASTA: Analyzing complex input
Start Read line Save header Read line Concatenate to sequence Read line Header? No Yes Do something End of input? No End $line = <STDIN>; my $endOfInput = 0; while ($endOfInput==0) { # 1.1. Read sequence name from FASTA header if (substr($line,0,1) eq ">") { $name = substr($line,1); } else... # 1.2. Read sequence until next FASTA header $seq = ""; $line = <STDIN>; while (substr($line,0,1) ne ">") { $seq = $seq . $line; $line = <STDIN>; if (!defined($line)) { $endOfInput = 1; last; } } # 2. Do something... }
Start Read line Save header Read line Concatenate to sequence Read line Header? No Yes Do something End of input? No End ################################### # 1. Foreach sequence in the input my (@lines, $line, $name, $seq); $line = <STDIN>; chomp $line; my $endOfInput = 0; while ($endOfInput==0) { ################################ # 1.1. Read sequence name from FASTA header if (substr($line,0,1) eq ">") { $name = substr($line,1); } else { die "bad FASTA format"; } # 1.2. Read sequence until next FASTA header $seq = ""; $line = <STDIN>; chomp $line; # Read until next header or end of input while (substr($line,0,1) ne ">") { $seq = $seq . $line; $line = <STDIN>; if (!defined($line)) { $endOfInput = 1; last; } chomp $line; } ################################ # 2. Do something... }
Class exercise 4 • Write a script that reads lines of names and expenses:Yossi 6.10,16.50,5.00Dana 21.00,6.00Refael 6.10,24.00,7.00,8.00ENDFor each line print the name and the sum. Stop when you reach "END" • Change your script to read names and expenses on separate lines, Identify lines with numbers by a "+" sign as the first character in the string:Yossi+6.10+16.50+5.00Dana+21.00+6.00Refael +6.10+24.00+7.00+8.00END Hint:while … { $sum = $sum + $num} Use $line=<STDIN> in PerlExpress
Use $line=<STDIN> in PerlExpress Class exercise 4 • (Home Ex. 2 Q. 5) Write a script that reads several protein sequences in FASTA format, and prints the name and length of each sequence. Start with the example code from the last lesson. • 4*. Write a script that reads several DNA sequences in FASTA format, and printsFASTA output of the sequences whose header starts with 'Chr07'. (Use the example “genomic FASTA” from the webpage) • 5*. As in Q4, but now concatenate all the sequences whose header starts with 'Chr07'. • 6**. (Home Ex. 3) Write a script that reads several DNA sequences in FASTA format,and print for each FASTA record its header and its G+C content.