170 likes | 409 Views
Revision: variables & arrays. Array declaration my @array; Array initialization @array = ('A','B','C','D'); @array = (3..7); Array element : print $array[1]; B $array[0] = '*'; *BCD Array size: print scalar(@aray); 4. Reading a single line : my $ line = <STDIN>;
E N D
Revision: variables & arrays Array declarationmy @array; Array initialization@array = ('A','B','C','D'); @array = (3..7); Array element: print $array[1]; B $array[0] = '*'; *BCD Array size: print scalar(@aray); 4
Reading a singleline: my $line = <STDIN>; Reads a single line End input with Enter First line Reading a listof lines: my @lines = <STDIN>; Each line is a different element in an array Hit Ctrl-z to end input First line Second Last Ctrl-z $line @lines "First line\n" 0 1 2 "First line\n" "Second\n" "Last\n" Revision: reading input
0 1 2 3 "A" "B" 17 "hi" Revision: arrays function Unshift & shiftmy @a = @start; unshift(@a,"*"); print "@a"; * A B 17 hi @a = @start; my $x = shift(@a); print @a; B 17 hi print $x; A @start=("A","B",17,"hi"); @start Push &pop @a = @start; push(@a,"Z"); print @a; A B 17 hi Z @a = @start; my $x = pop(@a); print @a; A B 17 print $x; hi Note: These functions change the array (argument)
Revision: arrays function (2) $str = "So-long-and-thanks-for-all-the-fish"; @a = split(/-/, $str); $str = join("!! ", @a ); print "$str\n"; So!! Long!! and!! thanks!! for!! all!! the!! Fish reverse(1,2,3); sort("b","a","d","c",); Note: These functions do NOT change the array (argument), But return the “manipulated” array
Controls allow non-sequential execution of commands, and responding to different conditions. Controls: if ? Note the indentation:a single tab in each line of new block print "How old are you?\n";my $age = <STDIN>;if ($age < 18) { print "Sorry, I’m not allowed to chat with minors\n";} else { print "Are you doing anything tomorrow night?\n";} ‘}’ that ends the block should be in the same indentation as where it started
It’s convenient to test several conditions in one if structure: if ($age < 18) { print "Sorry, I’m not allowed to chat with minors"; print "\n"; } elsif ($age < 25) { print "Are you doing anything tomorrow night?\n"; } elsif ($age < 35) { print "Are you married?\n"; } else { print "Do you need help crossing the street?\n"; } if, elsif, else
Comparison operators if ($age == 18){ ... } if ($name eq "Yossi")... if ($name ne "Yossi")... if ($name lt "n")... if ($age = 18)... Found = in conditional, should be == at ... if ($name == "Yossi")... Argument "Yossi" isn't numeric in numeric eq (==) at ...
and && or || not ! Boolean operators if (($age==18) and ($name eq "Yossi")){ print "Hi Yossi18"; } if (($age==18) or ($name eq "Yossi")){ print ". . ."; } if (!($name eq "Yossi")){ ... }
Class exercise 3a Ask the user for his grades average and: • print "wow!" if it is above 90. • print "wow!" if it is above 90; "well done." if it is above 80 and "oh well" if it is lower. • print "wow!" if it is above 90; "well done." if it is above 80 and "oh well" if it is lower. Print an error massage if the number is negative or higher than 100 (Use the or operator). 4*. print "boom" if the number is divisible by 7. (The % operator gives the remainder (שארית) . [$x % $y gives the remainder of $x divided by $y] for example 13%7→6, 14%7 → 0) 5*. print "mega boom" if the number is divisible by 7 and by 2, or it equals 99.
Commands inside a loop are executed repeatedly (iteratively). The while loop is "repetitive if": executed while the condition holds. Loops: while my $num=2; while ($num < 1000) { print "$num\n"; $num = $num*2; } my $name=<STDIN>; chomp($name); print "Guess a name...\n"; while ($name ne "Yossi") { $name = <STDIN>; chomp($name); } print "Indeed!\n";
The foreach loop passes through all the elements of an array Loops: foreach my @nameArr = ("Yossi","Daiana","Jojo"); my $name; foreach $name (@nameArr) { print "Hello $name!\n" } Reminder: $#names is the index of the last element of @names Compare with: my $index=0; while ($index <= $#nameArr) { print "Hello $nameArr[$index]\n"; $index++; }
The foreach loop passes through all the elements of an array my @numArr = (1,1,2,3,5); foreach my $number (@numArr) { $number++; } Loops: foreach Note: The array is actually changed
Fasta format Fasta format sequence begins with a single-line description, that start with '>', followed by lines of sequence data that contains new-lines after a fixed number of characters: >gi|229608964|ref|NM_014600.2| Homo sapiens EH-domain AAACATGGCGGCGCCCTGCGCGGCTTCCCGTCGCCGCAACCGTGGGGCCGGCCCTGCCTTGGAGCGGAGCCGAAGCATCCCTTGCTGCACGCAGGGCAGAGCAGGCGAGGGCTGGGGGCCGTATAACTTATTTTATATCCATATTCAGACTATATAGAGAATATTCTATGCATCTATGACGTGCTTAC>gi|197099147|ref|NM_001131576.1| Pongo abelii EH-domain AGAGCTGAGCGCCTGCCCACAAACATGGCGGCGCCCTGCGCGGCTTCCCTTCGCCGGGACCGCCTGGGGCTGCAGGATGCTGCTGCGGATGCTGAGCTGTCCGCGGGTTGGGCAGCGTCGCTGCGCGGCTTCCCTT>gi|55742034|ref|NM_001006733.1| Xenopus tropicalis EH-domain CGGGCAAGACCACCTTCATCCGCCACCTCATAGAGCAGGACTTCCCCGGCATGAGGATCGGGCCCGAACCGGGGACTTCCTCTGCGCGCCGGCTTCCTGCCCAGCTGGCATTTAAACCACACATGGCGGCGCCCTGCGCGGCTTCCCGTCGCCGCAACCGTGGGGCCGGCC
Breaking out of loops next– skip to the next iteration last– skip out of the loop my @lines = <STDIN>; 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
Class exercise 3b • Read a list of numbers (on separate lines) and print each number multiplied by 10. • Read several protein sequences in FASTA format, and print only their header lines. (see example FASTA file on the course webpage). • Read a line containing numbers separated by spaces and print the sum of these numbers. 4*. Read list of full names (first and last name), one in each line, until "END" is entered. Print out a list of sorted last names.