1 / 32

Perl Syntax: control structures

Perl Syntax: control structures. Learning Perl, Schwartz. FYI. http://www.nimblegen.com/. Outline. Control structures if, for, while, foreach File Handles and Streams Regular Expressions -- a start. Expression Evaluation for Control Structures. if (CONDITION) {

don
Download Presentation

Perl Syntax: control structures

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. Perl Syntax: control structures Learning Perl, Schwartz

  2. FYI http://www.nimblegen.com/

  3. Outline • Control structures • if, for, while, foreach • File Handles and Streams • Regular Expressions -- a start

  4. Expression Evaluation for Control Structures if (CONDITION) { ….. lines in this block will be executed } CONDITIONS evaluate as true: • any string except "" and "0" • Any number except 0 • Any reference is true All undefined values are false

  5. Control Structures Provides the capability to make decisions based on results if( something is true) do this else do something else

  6. if Control Structure if (condition) { execute block } elsif (condition) { } elsif (condition) { } : : else { }

  7. if Control Structure if ( $i > 60) { print "Length of line is ".length($line)." \n"; } if ($sequence eq 'ATG') { $start_index = $x; $j =$j + 1; } else { print "Start codon not found."; } Note that in C -- you can have a single line of execution following an "if" if(j > 60) printf ("print statement here \n",j);

  8. Examples if ($sequence eq 'ATG') { $start_index = $x; $j = $j + 1; } elsif ($sequence eq 'ATT') { $stop_location = $y; } else { print "Start/stop codons not found."; }

  9. Block/Braces Conventions if ($i == 5) { # this is how most of the world writes code print "Hello world.\n"; } else { $x=0; } ####################################### if ($i == 5) # this is an alternative sometimes used { # either way is purely a stylistic choice print "Hello world.\n"; } else { $x=0; }

  10. Iterative/Looping Control Structures while (condition) { #block is repeatedly ….block # executed as long as } # condition is TRUE while ($count < 300) { $count++; # same as $count = $count + 1; $number = <STDIN>; &calc_sales($number); }

  11. Strange Example #!/usr/bin/perl ## hellow world #comments $i = "ATG"; $j = "TTTG"; if($i == $k) { print "Prof braun was wrong\n"; }

  12. for loop for( initialization; condition; execute) { block } • Initialization statement is executed, first, only once • CONDITION is evaluated, if true BLOCK is executed • EXECUTE is performed • Go to 2)

  13. for loop for( initialization; expression; execute) { block } Equivalent to while loop initialization; while (expression){ block execute }

  14. for loop examples (nearly identical to C structure) for ($i=0; $i<4; $i = $i+1) { print "Counting $i \n"; } print "Out of loop: $i \n"; Counting 0 Counting 1 Counting 2 Counting 3 Out of loop: 4

  15. for loop examples for (my $i=0; $i<4; $i = $i+1) { print "Counting $i \n"; } print "Out of loop: $i \n"; Counting 0 Counting 1 Counting 2 Counting 3 Out of loop: for( ; $i<10 ; ) { # expressions are optional – none == infinite loop }

  16. foreach Really no equivalent in C. foreach SCALAR ( ARRAY) { } @nts = (A,T,C); foreach $nucleotide (@nts) { print $nucleotide ."\n"; } A T C

  17. Side Notes auto increment/decrement $i++; #same as $i = $i +1 $i--; chomp(STRING) and chop(STRING) chomp removes only 1 newline at end of string or variable (if one exists) chop removes last character of string

  18. STDIN, STDOUT, Filehandles STDIN -- input "stream" (ex. keyboard -- are there others?) STDOUT -- output "stream" (ex. screen) Filehandles -- similar to streams -- input from, and output to files

  19. More Side Notes chomp ($text = <STDIN>); $text = <STDIN>; chomp($text); # defined EXPR ; # returns a boolean value saying whether EXPR has a real value or not -- scalar with non-zero string or numeric $text = "hello"; if (defined($text)) { : } May be used to determine if subroutines or filehandle exists.

  20. Filehandles • name for a file, device, socket, or pipe • filehandle name is arbitrary (one of few examples where $ is NOT used) open(FILE, "filename"); open(UP, "test"); # read from file "test" open(WRITE,">filename"); # write to file "filename" open(NOW, ">>test"); # append to a file close (FH);

  21. Filehandles Examples # STDOUT is default output – to screen # STDIN is default input – from keyboard print STDOUT "Enter a number: "; $num = <STDIN>; print STDOUT "the number is $num\n"; open(BOB, "test"); $line = <BOB>; #read one line from "test" open(NEW,">output"); print NEW "$line"; #write $line to file "output"

  22. Pipes and File redirection | -- pipe operator (Unix) Example: File ("names") contains: Tim Tracy Bob cat names | sort > look Output: Bob Tim Tracy cat names | sort | wc > look > -- redirection operator ls > look Places files and directories into the file named “look”

  23. Regular Expressions (regexp) • Regular Expression – template that matches a set of strings • very useful for text processing and pattern matching • document and text processing – text editors, etc • formatted and unformatted data • genomic data (sequences, ESTs, genes, microarray, etc) >gi|25952121|ref|NM_033028.2| Homo sapiens Bardet-Biedl syndrome 4 (BBS4), mRNA GACTTCCGGCCGCGCAGCGGTGGGCTGAGCTAAAATGGCTGAGGAGAGAGTCGCGACGAGAACTCAATTT CCTGTATCTACTGAGTCTCAAAAACCCCGGCAGAAAAAAGCTCCAGAGTTTCCTATTTTGG… • default variable: $_ • in many cases, Perl will automatically use $_ as the default variable Examples: $_ = "test\n"; print; # prints $_ by default foreach (1..5) { # no variable specified print "Count $_ \n"; } $_ will show up more in later lectures

  24. Pattern Matching To compare a pattern (regular expression): $_ = "ATCGAGAGCATGCCATGCAT"; if(/ATG/) { print "Found sequence\n"; } Remember naïve.pl

  25. Regular Expressions metacharacters (.) period – matches any character except newline (\) backslash – makes any metacharacter a non-metacharacter Example: 3.145 would match 3b145 3\.145 would match 3.145

  26. Regular Expressions Quantifier metacharacters (*) asterisk – match preceding item 0 or more times /ATGC*ATG/ matches ATGATG, ATGCATG, ATGCCCCCCCCCCCCCCCCCCCCATG, etc. (+) plus – match preceding item 1 or more times (?) question mark – match preceding item 0 or 1 times /ATGC?ATG/ only matches ATGCATG and ATGATG

  27. General Quantifiers /a{5,15}/ matches between 5, and 15 "a" matches if first 15, if more than 15 "a" /(fred){3,}/ matches 3 or more "fred" with no upper limit – fredfredfred /(fred){3}/ matches exactly fredfredfred * = {0,} zero or more + = {1,} one or more ? = {0,1} zero or one

  28. #!/usr/bin/perl #ping for 1 week #every 5 minutes #12 per hour = 2016 pings $|=1; #output auto flush $num_pings=2016; $count = 0; $date = `date /t`; # Wed 09/17/2008 chomp($date); $date =~ s/(\w+)\s(\d+)\/(\d+)\/(\d+)(\s+)/$2$3$4/; $file = ">>PingLog".$date.".txt"; print "file = $file\n"; while($count<$num_pings) { print "pinging\n"; open(PING,$file); #$results = `ping 12.217.250.200`; $results = `ping 128.255.22.207`; #$results = system("ping 128.255.22.207"); print "results = $results\n"; $date = `date /t`; $time = `time /t`; $_ = $results; if(m/Reply/) { print PING "Success: $date $time $results\n"; } else { print PING "FAIL: $date $time $results\n"; } print PING "---------------------------------------\n"; close(PING); $count++; $ticks = 150; # 2 * 150 = 300 secs = 5 mins while($ticks>0) { print "$count $ticks \r"; sleep(2); $ticks--; } print "\n"; }

  29. FAIL: Wed 04/30/2008 08:33 PM Pinging 128.255.22.207 with 32 bytes of data: Request timed out. Request timed out. Request timed out. Request timed out. Ping statistics for 128.255.22.207: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss), --------------------------------------- Success: Wed 04/30/2008 08:38 PM Pinging 128.255.22.207 with 32 bytes of data: Reply from 128.255.22.207: bytes=32 time=22ms TTL=51 Reply from 128.255.22.207: bytes=32 time=21ms TTL=51 Reply from 128.255.22.207: bytes=32 time=23ms TTL=51 Reply from 128.255.22.207: bytes=32 time=21ms TTL=51 Ping statistics for 128.255.22.207: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 21ms, Maximum = 23ms, Average = 21ms ---------------------------------------

  30. ping 128.255.22.1 What the output of "ping" should look like: Pinging 128.255.22.1 with 32 bytes of data: Reply from 128.255.22.1: bytes=32 time=24ms TTL=52 Reply from 128.255.22.1: bytes=32 time=24ms TTL=52 Reply from 128.255.22.1: bytes=32 time=24ms TTL=52 Reply from 128.255.22.1: bytes=32 time=24ms TTL=52 Ping statistics for 128.255.22.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 22ms, Maximum = 25ms, Average = 23ms

  31. Ping failure Pinging 128.255.22.1 with 32 bytes of data: Request timed out. Request timed out. Request timed out. Request timed out. Ping statistics for 128.255.22.1: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

  32. END

More Related