More loops

1 / 14

# More loops - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' More loops ' - ingo

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### More loops

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

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:
• \$studentNameArray
• We will see conventions for other kinds of variables and names as we continue.

### Naming variables

Start

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

Concatenate to sequence

No

Yes

Do something

End of input?

No

End

• Overall design:
• Do something
• Let’s see how it’s done…

### FASTA: Analyzing complex input

Start

Concatenate to sequence

No

Yes

Do something

End of input?

No

End

\$line = <STDIN>;

my \$endOfInput = 0;

while (\$endOfInput==0) {

if (substr(\$line,0,1) eq ">") {

\$name = substr(\$line,1);

} else...

\$seq = "";

\$line = <STDIN>;

while (substr(\$line,0,1) ne ">") {

\$seq = \$seq . \$line;

\$line = <STDIN>;

if (!defined(\$line)) {

\$endOfInput = 1;

last;

}

}

# 2. Do something...

}

Start

Concatenate to sequence

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

################################

if (substr(\$line,0,1) eq ">") {

\$name = substr(\$line,1);

} else {

}

\$seq = "";

\$line = <STDIN>;

chomp \$line;

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.