Presentation is loading. Please wait.

Presentation is loading. Please wait.

I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){

Similar presentations


Presentation on theme: "I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){"— Presentation transcript:

1 I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){ exit(1); }

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

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

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 Subroutines (2) $k= &max (9,20); sub max { if( $_[0] > $_[1]){ $_[0]; #or “return $_[0];” }else{ $_[1]; # or “return $_[1];” } $k= &max (9,20,30); $k= &max (9); # $_[1] becomes undef

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 my $i=1; &inc( $i ); sub inc{ my($i)=@_; $i++; } #value of $i is not changed my $i=1; &inc( \$i ); sub inc{ my($i)=@_; $$i++; } Passing Values by Reference

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 @array=(1,2,3,4); $rarray= \@array; $rrarray = \$rarray; print ref $rarray, " ", ref $rrarray, "\n"; Output: ARRAY REF REF ARRAY SCALAR HASH CODE (function) GLOB (file)

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.


Download ppt "I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){"

Similar presentations


Ads by Google