1 / 21

I/O

I/O. while (&lt;STDIN&gt;){ #remove new line char <br> chomp($_); if($_ eq “quit”){ exit(1); } }. while ($line= &lt;STDIN&gt;){ #remove new line char <br> chomp($line); if($line eq “quit”){ exit(1); } }. I/O (2). # @ARGV array of program arguments foreach $file ( @ARGV ){

cecil
Download Presentation

I/O

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. I/O while (<STDIN>){ #remove new line char \n chomp($_); if($_ eq “quit”){ exit(1); } } while ($line= <STDIN>){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } }

  2. I/O (2) • # @ARGV array of program arguments • foreach $file ( @ARGV ){ • open FILE, $file; • while (<FILE>){ • print $_; • } • } foreach $file ( @ARGV ){ open FILE, $file; print <FILE>; }

  3. I/O (3) # does the same as the previous example # (similar to cat Unix program) while (<>){ print $_; } #sorts print sort <>; #even more simple print <>;

  4. Filehandles open FILE, “tmp.fasta”; #open to read or open FILE, “<tmp.fasta”; #open to read open FILE, “>tmp.fasta”; #open to write open FILE, “>>tmp.fasta”; #append print FILE, “new line\n”;

  5. Filehandles (2) open FILE, “readme.txt” or die “Cannot open file: $!”; unless( -e $filename){ print “File $filename doesn’t exist\n”; }

  6. File tests

  7. Filehandles; Globing Unix Prompt: ls *.txt @txt_files= glob “ *.txt ”; @txt_files= glob “ .* *.txt ”; foreach ( glob(“*.fasta”) ){ print “File name: $_\n”; }

  8. Directory Handles opendir DIR, $dirname or die “Cannot open dir $dirname\n”; foreach $file (readdir DIR){ print $file.”\n”; } closedir DIR; chdir “/home/usr/” or die “Couldn’t change directory: $!”; @files = readdir DIR; $” = “\n”; print "@files";

  9. Subroutines • Subroutine definition is global. • Global variables can be used within the subroutine (not a good style). • Paramters are stored in the default array @_ • $_[0], $_[1], … • The last evaluated expression is the return value. • (return will also work)

  10. $k= &max (9,20,30); $k= &max (9); # $_[1] becomes undef Subroutines (2) $k= &max (9,20); sub max { if( $_[0] > $_[1]){ $_[0]; #or “return $_[0];” }else{ $_[1]; # or “return $_[1];” } }

  11. Subroutines (3) sub max { ($a, $b)= @_; #might change global variables if( $a > $b){ $a; }else{ $b; } } my($a, $b)= @_; #private variables

  12. Subroutines (4) $max_value = &max ($i,$j,$k,$l); sub max { my($max_so_far)= shift @_ ; foreach ( @_ ){ if($_ > $max_so_far){ $max_so_far = $_ ; } } return $max_so_far; } my($max_so_far)= pop @_;

  13. use strict use strict; #from now on use strict in all assignments $i=1; #compiler will report error my $i=1; #ok sub f { $n=3; #compiler will report error my $n=3; #ok }

  14. Passing Values by Reference my $i=1; &inc( \$i ); sub inc{ my($i)=@_; $$i++; } my $i=1; &inc( $i ); sub inc{ my($i)=@_; $i++; } #value of $i is not changed

  15. References References: $rscalar = \$scalar; $rarray = \@array; $rhash = \%hash; $rfile = \*FILE; $rcode = \&qsort; Dereferences: print $$rscalar; #or print ${$rscalar}; print @$rarray; #or print @{$rarray}; %$rhash{“key”}= “value”; print $rfile “test \n”; &$qsort( \@array ); $$array[3]; #OR $array->[3];

  16. Reference to Array @array= (“one”, “two”, “three”); $aref= \@array; print $$aref[0]; foreach ( @$aref ){ print; }

  17. Reference to Array (2) @array= (“one”, “two”, “three”); $r= @array; #array size ($r) = (“one”, “two”, “three”); #first list element -> one $r= (“one”, “two”, “three”); #last computed value->three $r = \@array; $r= [ “one”, “two”, “three” ]; #array creation, pointer to anonymous array print $$r[0]; $r= [ “one”, “two”, [ 1, 2, 3] ];

  18. Reference to File open LOG, “>log.txt”; writelog( \*LOG, “process is running”); sub writelog{ my $LOG = shift; print $LOG scalar( localtime(time) ), “:”, @_; }

  19. Reference to Function open LOG, “>log.txt”; $r= \&writelog; &$r(\*LOG, “process is running”); sub writelog{ my $LOG = shift; print $LOG scalar( localtime(time) ), “:”, @_; }

  20. Ref operator REF ARRAY SCALAR HASH CODE (function) GLOB (file) @array=(1,2,3,4); $rarray= \@array; $rrarray = \$rarray; print ref $rarray, " ", ref $rrarray, "\n"; Output: ARRAY REF

  21. HomeWork Implement QuickSort (divide-and-conquer) Divide: The array A[p..r] is partitioned into two nonempty subarrays A[p..q] and A[q+1..r] such that each element of A[p..q] is less than or equal to each element of A[q+1..r]. The index q is computed as part of this procedure. Conquer: The two subarrays A[p..q] and A[q+1..r] are sorted by recursive calls to quicksort. Input: Strings with numbers separated by whitespaces. Output: Single string of sorted numbers separated by space.

More Related