How Perl Saved my Day
Previous First Next

Example 2

The solution

#!/usr/local/bin/perl           # the home of Perl

@files  = <*.pr1>;              # put a list of all files
                                # ending in .pr1 into the array @files

foreach $file (@files) {        # for each element of the array @files
                                # (i.e., each file called $file)
  $phenol   = 0;                # initialize variables
  $php      = 0;
  @lines    = ();

  open(F, $file) or die;        # open the file named $file, quit if unsuccessfull
  while (<F>) {                 # read in line after line, and store it in $_[Note 3]
    s/^\s+//;                   # remove leading (^) white space (\s) from $_
    s/\s+$//;                   # remove trailing ($) white space (\s) from $_
    push @lines, $_;            # add the line ($_) to the array @lines
                                # which contains all lines of a file
    if  (/PHENOL/) {            # if the line contains PHENOL
      @columns  = split /\s+/;  # then split it at white spaces
      $phenol   = $columns[4];  # and store the peak area (5th column) in $phenol
    } elsif (/PHP/) {           # same for PHP
      @columns  = split /\s+/;
      $php    = $columns[4];
    }
  }
  close(F);                     # close the file named $file

  $lines[0] =~  /(\S+$)/;            # get the last word of line 1
  $sample   =   $1;                  # which is the sample name
  $lines[6] =~  /\s*(\S+)\s+(\S+)/;  # date and time are the
  $date   = $1;                      # first and second word on line 7
  $time   = $2;                                             
  print "$file  $sample  $date  ";   # print the result to standard output
  print "$time  $phenol  $php\n";
}

Previous First Next
10.12.1998 Michael Gfeller