210 likes | 392 Views
I/O. while (<STDIN>){ #remove new line char <br> chomp($_); if($_ eq “quit”){ exit(1); } }. while ($line= <STDIN>){ #remove new line char <br> chomp($line); if($line eq “quit”){ exit(1); } }. I/O (2). # @ARGV array of program arguments foreach $file ( @ARGV ){
E N D
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); } }
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>; }
I/O (3) # does the same as the previous example # (similar to cat Unix program) while (<>){ print $_; } #sorts print sort <>; #even more simple print <>;
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”;
Filehandles (2) open FILE, “readme.txt” or die “Cannot open file: $!”; unless( -e $filename){ print “File $filename doesn’t exist\n”; }
Filehandles; Globing Unix Prompt: ls *.txt @txt_files= glob “ *.txt ”; @txt_files= glob “ .* *.txt ”; foreach ( glob(“*.fasta”) ){ print “File name: $_\n”; }
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";
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)
$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];” } }
Subroutines (3) sub max { ($a, $b)= @_; #might change global variables if( $a > $b){ $a; }else{ $b; } } my($a, $b)= @_; #private variables
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 @_;
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 }
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
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];
Reference to Array @array= (“one”, “two”, “three”); $aref= \@array; print $$aref[0]; foreach ( @$aref ){ print; }
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] ];
Reference to File open LOG, “>log.txt”; writelog( \*LOG, “process is running”); sub writelog{ my $LOG = shift; print $LOG scalar( localtime(time) ), “:”, @_; }
Reference to Function open LOG, “>log.txt”; $r= \&writelog; &$r(\*LOG, “process is running”); sub writelog{ my $LOG = shift; print $LOG scalar( localtime(time) ), “:”, @_; }
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
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.