slide1
Download
Skip this Video
Download Presentation
I/O

Loading in 2 Seconds...

play fullscreen
1 / 21

I/O - PowerPoint PPT Presentation


  • 150 Views
  • Uploaded on

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 ){

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' I/O' - cecil


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1
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
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
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
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
Filehandles (2)

open FILE, “readme.txt”

or die “Cannot open file: $!”;

unless( -e $filename){

print “File $filename doesn’t exist\n”;

}

filehandles globing
Filehandles; Globing

Unix Prompt: ls *.txt

@txt_files= glob “ *.txt ”;

@txt_files= glob “ .* *.txt ”;

foreach ( glob(“*.fasta”) ){

print “File name: $_\n”;

}

directory handles
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
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)
subroutines 2

$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
Subroutines (3)

sub max {

($a, $b)= @_; #might change global variables

if( $a > $b){

$a;

}else{

$b;

}

}

my($a, $b)= @_; #private variables

subroutines 4
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

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

}

slide14

Passing Values by Reference

my $i=1;

&inc( \$i );

sub inc{

my($i)[email protected]_;

$$i++;

}

my $i=1;

&inc( $i );

sub inc{

my($i)[email protected]_;

$i++;

}

#value of $i is not changed

references
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
Reference to Array

@array= (“one”, “two”, “three”);

$aref= \@array;

print $$aref[0];

foreach ( @$aref ){ print; }

reference to array 2
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
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
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 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
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.

ad