Read and Write Files                        with PerlBioinformatics master course, „11/‟12   Paolo Marcatili
Resume •   Scalars                   $ •   Arrays                    @ •   Hashes                      % •   Foreach      ...
Agenda •   For •   Conditions •   Perl IO •   Open a File •   Write on Files •   Read from Files •   While loopBioinformat...
New cycle! for (my $i=0;$i<100;$i++){    print “$i n”; } Can we rewrite foreach using for? Hint: scalar @array is the arra...
Conditions if (condition){ Do something }Bioinformatics master course, „11/‟12   Paolo Marcatili
Conditions if ($time>5){ print “Time to go!n”; }Bioinformatics master course, „11/‟12   Paolo Marcatili
Conditions if ($day == 27){ print “Wage!n”; }Bioinformatics master course, „11/‟12   Paolo Marcatili
Conditions if ($day eq “Sunday”){ print “Alarm off!n”; } else{ print “snoozen”; }Bioinformatics master course, „11/‟12   P...
Perl IO                     (IO means Input/Output)Bioinformatics master course, „11/‟12         Paolo Marcatili
Why IO? Since now, Perl is #! /usr/bin/perl -w use strict; #<- ALWAYS!!! my $string=”All work and no play makes Jack a   d...
Why IO? But if we want to do the same with a user-submitted string?Bioinformatics master course, „11/‟12   Paolo Marcatili
Why IO? But if we want to do the same with a user-submitted string? IO can do this!Bioinformatics master course, „11/‟12  ...
IO types Main Inputs • Keyboard • File • Errors Main outputs • Display • FileBioinformatics master course, „11/‟12   Paolo...
More than tomatoes Let‟s try it: #! /usr/bin/perl -w   use strict; my $string=<STDIN>;   for (my $i=1;$i<100;$i++){    pri...
Rationale Read from and write to different media STDIN means standard input (keyboard)     ^      this is a handle <SMTH> ...
Handles Handles are just streams “nicknames” Some of them are fixed: STDIN     <-default is keyboard STDOUT <-default is d...
Open/Write a fileBioinformatics master course, „11/‟12   Paolo Marcatili
open We have to create a handle for our file open(OUT, “>”,”out.txt”) or die(“Error opening out.txt: $!”);        ^     N....
close When finished we have to close it: close OUT; If you dont, Santa will bring no gift.Bioinformatics master course, „1...
Print OUT #! /usr/bin/perl -w use strict; open(OUT, ">”,”out.txt") || die("Error opening out.txt: $!"); print "type your c...
Read from FilesBioinformatics master course, „11/‟12   Paolo Marcatili
Read open(IN, “<song.txt”) or die(“Error opening song.txt: $!”); print <IN>; print <IN>; print <IN>; print <IN>; print <IN...
Read with loops Problems: • It‟s long • File size unknown solution: Use loopsBioinformatics master course, „11/‟12   Paolo...
While loopBioinformatics master course, „11/‟12   Paolo Marcatili
While while (condition){               do something… }Bioinformatics master course, „11/‟12      Paolo Marcatili
While - for differences   While                                For   • Undetermined                       > Determined   •...
While example   Approx. solution of x^2-2=0   (Newton‟s method)   my $sol=0.5;   my $err=$sol**2-2;   while ($err**2>.001)...
Read with while   #! /usr/bin/perl -w   use strict;   open(MOD, "<IG.pdb") || die("Error opening      IG.pdb: $!");   whil...
Redirect outputsBioinformatics master course, „11/‟12   Paolo Marcatili
Redirections     #!/usr/bin/perl     open(STDOUT, ">foo.out") || die "Cant redirect stdout";     open(STDERR, ">&STDOUT") ...
@ARGVBioinformatics master course, „11/‟12   Paolo Marcatili
Command Line Arguments •   Command line arguments in Perl are extremely easy. •   @ARGV is the array that holds all argume...
Quick Program with @ARGV • Simple program called log.pl that takes in a number and   prints the log base 2 of that number;...
$_ • Perl default scalar value that is used when a   variable is not explicitly specified. • Can be used in      – For Loo...
$_ and For Loops •   Example using $_ in a for loop      @array = ( “Perl”, “C”, “Java” );      for(@array) {          pri...
$_ and File Handlers • Example in using $_ when reading in a file;      while( <> ) {           chomp $_;               # ...
Opendir, readdirBioinformatics master course, „11/‟12   Paolo Marcatili
Opendir& readdir     • Just like open, but for dirs     # load all files of the "data/" folder into the @files array     o...
Upcoming SlideShare
Loading in...5
×

Master perl io_2011

409

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
409
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Master perl io_2011

  1. 1. Read and Write Files with PerlBioinformatics master course, „11/‟12 Paolo Marcatili
  2. 2. Resume • Scalars $ • Arrays @ • Hashes % • Foreach |-@ $ • Length … • Split $@Bioinformatics master course, „11/‟12 Paolo Marcatili
  3. 3. Agenda • For • Conditions • Perl IO • Open a File • Write on Files • Read from Files • While loopBioinformatics master course, „11/‟12 Paolo Marcatili
  4. 4. New cycle! for (my $i=0;$i<100;$i++){ print “$i n”; } Can we rewrite foreach using for? Hint: scalar @array is the array sizeBioinformatics master course, „11/‟12 Paolo Marcatili
  5. 5. Conditions if (condition){ Do something }Bioinformatics master course, „11/‟12 Paolo Marcatili
  6. 6. Conditions if ($time>5){ print “Time to go!n”; }Bioinformatics master course, „11/‟12 Paolo Marcatili
  7. 7. Conditions if ($day == 27){ print “Wage!n”; }Bioinformatics master course, „11/‟12 Paolo Marcatili
  8. 8. Conditions if ($day eq “Sunday”){ print “Alarm off!n”; } else{ print “snoozen”; }Bioinformatics master course, „11/‟12 Paolo Marcatili
  9. 9. Perl IO (IO means Input/Output)Bioinformatics master course, „11/‟12 Paolo Marcatili
  10. 10. Why IO? Since now, Perl is #! /usr/bin/perl -w use strict; #<- ALWAYS!!! my $string=”All work and no play makes Jack a dull boyn"; for (my $i=1;$i<100;$i++){ print $string; }Bioinformatics master course, „11/‟12 Paolo Marcatili
  11. 11. Why IO? But if we want to do the same with a user-submitted string?Bioinformatics master course, „11/‟12 Paolo Marcatili
  12. 12. Why IO? But if we want to do the same with a user-submitted string? IO can do this!Bioinformatics master course, „11/‟12 Paolo Marcatili
  13. 13. IO types Main Inputs • Keyboard • File • Errors Main outputs • Display • FileBioinformatics master course, „11/‟12 Paolo Marcatili
  14. 14. More than tomatoes Let‟s try it: #! /usr/bin/perl -w use strict; my $string=<STDIN>; for (my $i=1;$i<100;$i++){ print $string; }Bioinformatics master course, „11/‟12 Paolo Marcatili
  15. 15. Rationale Read from and write to different media STDIN means standard input (keyboard) ^ this is a handle <SMTH> means “read from the source corresponding to handle SMTH”Bioinformatics master course, „11/‟12 Paolo Marcatili
  16. 16. Handles Handles are just streams “nicknames” Some of them are fixed: STDIN <-default is keyboard STDOUT <-default is display STDERR <-default is display Some are user defined (files)Bioinformatics master course, „11/‟12 Paolo Marcatili
  17. 17. Open/Write a fileBioinformatics master course, „11/‟12 Paolo Marcatili
  18. 18. open We have to create a handle for our file open(OUT, “>”,”out.txt”) or die(“Error opening out.txt: $!”); ^ N.B. : it‟s user defined, you decide it Tip “<“,”out.txt” <- read from out.txt “>”,”out.txt” <- write into out.txt “>>”,”out.txt” <- append to out.txtBioinformatics master course, „11/‟12 Paolo Marcatili
  19. 19. close When finished we have to close it: close OUT; If you dont, Santa will bring no gift.Bioinformatics master course, „11/‟12 Paolo Marcatili
  20. 20. Print OUT #! /usr/bin/perl -w use strict; open(OUT, ">”,”out.txt") || die("Error opening out.txt: $!"); print "type your claim:n"; my $string=<STDIN>; for (my $i=1;$i<100;$i++){ print OUT $string; } close OUT; Now let‟s play with <,>,>> and file permissionsBioinformatics master course, „11/‟12 Paolo Marcatili
  21. 21. Read from FilesBioinformatics master course, „11/‟12 Paolo Marcatili
  22. 22. Read open(IN, “<song.txt”) or die(“Error opening song.txt: $!”); print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; print <IN>; close IN;Bioinformatics master course, „11/‟12 Paolo Marcatili
  23. 23. Read with loops Problems: • It‟s long • File size unknown solution: Use loopsBioinformatics master course, „11/‟12 Paolo Marcatili
  24. 24. While loopBioinformatics master course, „11/‟12 Paolo Marcatili
  25. 25. While while (condition){ do something… }Bioinformatics master course, „11/‟12 Paolo Marcatili
  26. 26. While - for differences While For • Undetermined > Determined • No counter > CounterBioinformatics master course, „11/‟12 Paolo Marcatili
  27. 27. While example Approx. solution of x^2-2=0 (Newton‟s method) my $sol=0.5; my $err=$sol**2-2; while ($err**2>.001){ $sol-=($sol**2-2)/(2*$sol); $err=$sol**2-2; print “X is $solnError=$errn”; }Bioinformatics master course, „11/‟12 Paolo Marcatili
  28. 28. Read with while #! /usr/bin/perl -w use strict; open(MOD, "<IG.pdb") || die("Error opening IG.pdb: $!"); while (my $line=<MOD>){ print substr($line,0,6)."n"; } close MOD;Bioinformatics master course, „11/‟12 Paolo Marcatili
  29. 29. Redirect outputsBioinformatics master course, „11/‟12 Paolo Marcatili
  30. 30. Redirections #!/usr/bin/perl open(STDOUT, ">foo.out") || die "Cant redirect stdout"; open(STDERR, ">&STDOUT") || die "Cant dup stdout"; select(STDERR); $| = 1; # make unbuffered select(STDOUT); $| = 1; # make unbuffered close(STDOUT); close(STDERR);Bioinformatics master course, „11/‟12 Paolo Marcatili
  31. 31. @ARGVBioinformatics master course, „11/‟12 Paolo Marcatili
  32. 32. Command Line Arguments • Command line arguments in Perl are extremely easy. • @ARGV is the array that holds all arguments passed in from the command line. – Example: • % ./prog.pl arg1 arg2 arg3 – @ARGV would contain (arg1, arg2, arg3) • $#ARGV returns the number of command line arguments that have been passed. – Remember $#array is the size of the array -1 !Bioinformatics master course, „11/‟12 Paolo Marcatili
  33. 33. Quick Program with @ARGV • Simple program called log.pl that takes in a number and prints the log base 2 of that number; #!/usr/local/bin/perl -w $log = log($ARGV[0]) / log(2); print “The log base 2 of $ARGV[0] is $log.n”; • Run the program as follows: – % log.pl 8 • This will return the following: – The log base 2 of 8 is 3.Bioinformatics master course, „11/‟12 Paolo Marcatili
  34. 34. $_ • Perl default scalar value that is used when a variable is not explicitly specified. • Can be used in – For Loops – File Handling – Regular ExpressionsBioinformatics master course, „11/‟12 Paolo Marcatili
  35. 35. $_ and For Loops • Example using $_ in a for loop @array = ( “Perl”, “C”, “Java” ); for(@array) { print $_ . “is a language I known”; } – Output : Perl is a language I know. C is a language I know. Java is a language I know.Bioinformatics master course, „11/‟12 Paolo Marcatili
  36. 36. $_ and File Handlers • Example in using $_ when reading in a file; while( <> ) { chomp $_; # remove the newline char @array = split/ /, $_; # split the line on white space # and stores data in an array } • Note: – The line read in from the file is automatically store in the default scalar variable $_Bioinformatics master course, „11/‟12 Paolo Marcatili
  37. 37. Opendir, readdirBioinformatics master course, „11/‟12 Paolo Marcatili
  38. 38. Opendir& readdir • Just like open, but for dirs # load all files of the "data/" folder into the @files array opendir(DIR, ”$ARGV[0]"); @files = readdir(DIR); closedir(DIR); # build a unsorted list from the @files array: print "<ul>"; foreach $file (@files) { next if ($file eq "." or $file eq ".."); print "<li><a href="$file">$file</a></li>"; } print "</ul>";Bioinformatics master course, „11/‟12 Paolo Marcatili
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×