I/O
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

I/O PowerPoint PPT Presentation


  • 101 Views
  • Uploaded on
  • Presentation posted in: General

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

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.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


I o

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”;

    }


    I o

    File tests


    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

    }


    I o

    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

    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.


  • Login