1 / 14

More loops

More loops. Commands inside a loop are executed repeatedly (iteratively): my $num=0; print &quot;Guess a number.<br>&quot;; while ($num != 31) { $num = &lt;STDIN&gt;; } print &quot;correct!<br>&quot;;. Loops. my @names = &lt;STDIN&gt;; chomp(@names); my $name; foreach $name (@names) { print &quot;Hello $name!<br>&quot;; }.

ingo
Download Presentation

More loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More loops

  2. 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";}

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

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

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

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

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

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

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

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

  11. 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... }

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

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

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

More Related