Notes: Perl Lab 04

Outline

  1. Opening files (read-only)
  2. Opening files (write)
  3. Opening files (append)
  4. foreach loops
  5. while loops
  6. split

  1. Opening files

    Files may be opened with the open function. The syntax is:
          open FILEHANDLE, "MODE filename"
          
    The FILEHANDLE identifier will be used to reference the file for read or writes. It can be named anything but is generally written in upper case. The MODE sets whether the file is opened with read-only access ('<'), write access ('>') or write-append ('>>'). These mode symbols are similar to the Unix shell redirection symbols. Examples of each mode are shown below. The filename can be the absolute or relative paths to the file to be opened.

  2. Opening Files with Read-only Access

    If no mode is specified, file opens will default to read-only access.
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab04 Example 00
    # Description: An example of opening a file with read access
    #==========================================================================
    
    # set the full path to the file to open
    $data_dir = '/mnt/homes/CLASSES/MET4720/LAB04';
    $datafile = $data_dir.'/metar.dat';
    
    # open the file in "read only" mode
    open(DF, "$datafile") || die "cannot open file: $!";
    
    
    # read the contents of the file...
    while($line = <DF>) {
        chomp($line);
        # print the line to file if it contains the text string "BTV"
        if($line =~ /BTV/) {
            print "$line\n";
        }
    }
    
    # close the file for reading
    close(DF);
    
    exit;
    # DONE
    
    

     
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> 
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_0.pl | head -3
    KBTV 232354Z 31010KT 1/4SM +SN FZFG BKN003 BKN007 OVC030 M02/M02 A3016 RMK AO2 SNE11B33 SLP220 4/001 P0003 60003
    KBTV 240006Z 29005G15KT 1/4SM +SN FZFG BKN001 OVC005 M01/M02 A3017 RMK AO2 P0002
    KBTV 232354Z 31010KT 1/4SM +SN FZFG BKN003 BKN007 OVC030 M02/M02 A3016=
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> 
    

    Below is an example showing the specified access ("read-only") and a method of capturing the contents of a file into an array.
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab04 Example 00
    # Description: An example of opening a file with read access
    #==========================================================================
    
    # set the full path to the file to open
    $data_dir = '/mnt/homes/CLASSES/MET4720/LAB04';
    $datafile = $data_dir.'/metar.dat';
    
    # open the file in "read only" mode
    open(DF, "<$datafile") || die "cannot open file: $!";
    
    
    # read the contents of the file into an array
    chomp(@lines = <DF>);
    
    # close the file for reading
    close(DF);
    
    foreach $line (@lines) {
        # print the line to file if it contains the text string "BTV"
        if($line =~ /BTV/) {
            print "$line\n";
        }
    }
    
    exit;
    # DONE
    
    

     
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> 
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_0a.pl | head -3
    KBTV 232354Z 31010KT 1/4SM +SN FZFG BKN003 BKN007 OVC030 M02/M02 A3016 RMK AO2 SNE11B33 SLP220 4/001 P0003 60003
    KBTV 240006Z 29005G15KT 1/4SM +SN FZFG BKN001 OVC005 M01/M02 A3017 RMK AO2 P0002
    KBTV 232354Z 31010KT 1/4SM +SN FZFG BKN003 BKN007 OVC030 M02/M02 A3016=
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> 
    

  3. Opening files for writing

    The ('>') mode will cause the file to be opened for write access. This will initially truncate the file to zero lenght if the file exists and create the file if it does not exist.
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab04 Example 01
    # Description: An example of opening a file for write access
    #==========================================================================
    
    # set the full path to the file to open
    $class_dir = '/mnt/homes/CLASSES/MET4720';
    $datafile = $class_dir.'/LAB04/metar.dat';
    $writefile = $class_dir.'/tuckerm/out.dat';
    
    # open the file in "read only" mode
    open(DF, "<$datafile") || die "cannot open file: $!";
    
    # open another file with write access to record our results
    open(OUT, ">$writefile") || die "cannot open file: $!";
    
    # read the contents of the file...
    while($line = <DF>) {
        chomp($line);
        # print the line to file if it contains the text string "BTV"
        if($line =~ /BTV/) {
            print OUT "$line\n";
        }
    }
    
    # close the file for reading
    close(DF);
    
    # close the file for writing
    close(OUT);
    
    exit;
    # DONE
    
    

     
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> 
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_1.pl
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> 
    

  4. Opening files for write/append

    Opening a file with the append mode is the same as opening it for writing except that if the file exists, it will not be overwritten but the pointer will be moved to the end of the existing file and all file writes will begin at this point.

     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab04 Example 02
    # Description: An example of opening a file for write/append access
    #==========================================================================
    
    # set the full path to the file to open
    $class_dir = '/mnt/homes/CLASSES/MET4720';
    $datafile = $class_dir.'/LAB04/metar.dat';
    $writefile = $class_dir.'/tuckerm/out.dat';
    
    # open the file in "read only" mode
    open(DF, "<$datafile") || die "cannot open file: $!";
    
    # open another file with write access to record our results
    open(OUT, ">>$writefile") || die "cannot open file: $!";
    
    # read the contents of the file...
    while($line = <DF>) {
        chomp($line);
        # print the line to file if it contains the text string "BTV"
        if($line =~ /BTV/) {
            print OUT "$line\n";
        }
    }
    
    # close the file for reading
    close(DF);
    
    # close the file for writing
    close(OUT);
    
    exit;
    # DONE
    
    

  5. foreach loops

    More to come....
  6. while loops

    More to come...
  7. Split

    split function splits a string into a list and returns the values as a list (think array). The syntax for split:

    split /PATTERN/, EXPR

    The PATTERN is a regular expression that split will look for to break the string apart into individual elements. EXPR is the string or function that will return a string value.

    A couple of simple split examples:
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab04 Example 03
    # Description: Simple examples of split
    #==========================================================================
    
    $first_string = " This is a simple string with spaces";
    $second_string = "55,42,34,48,86,67,71,12";
    
    # split into an array
    @new_array = split(/\s+/, $first_string);
    
    # show the elements
    print "Here is the first array\n";
    $counter = 0;
    foreach $item (@new_array) {
        print "$counter: $item\n";
        $counter ++;
    }
    
    # split into an array
    @another_array = split(/,/, $second_string);
    
    # show the elements
    print "Here is the second array\n";
    $counter = 0;
    foreach $item (@another_array) {
        print "$counter: $item\n";
        $counter ++;
    }
    
    exit;
    # DONE
    
    

    Below is the output from the previous script:
     
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> 
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_3.pl
    Here is the first array
    0: 
    1: This
    2: is
    3: a
    4: simple
    5: string
    6: with
    7: spaces
    Here is the second array
    0: 55
    1: 42
    2: 34
    3: 48
    4: 86
    5: 67
    6: 71
    7: 12
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
    

    An example of split used with handling data from a file:
     
    #!/usr/bin/perl -w
    
    # Name: Mark Tucker
    # Assignment: Lab04 Example 04
    # Description: Another example of split
    #==========================================================================
    
    # set the full path to the file to open
    $class_dir = '/mnt/homes/CLASSES/MET4720';
    $datafile = $class_dir.'/LAB04/metar.dat';
    
    # open the file in "read only" mode
    open(DF, "<$datafile") || die "cannot open file: $!";
    
    # read the contents of the file...
    while($line = <DF>) {
        chomp($line);
        # print the line to file if it contains the text string "BTV"
        if($line =~ /BTV/) {
            print "BTV:\n";
            @pieces = split(/\s+/, $line);
            $counter = 0;
            foreach $item (@pieces) {
                $counter++;
                print "\t$counter: $item\n";
            }
    
        }
    }
    
    # close the file for reading
    close(DF);
    
    exit;
    # DONE
    
    

    Here is a sampling of the output. Note that the output is truncated by the head command.
     
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04> ./lab04_4.pl | head
    BTV:
            1: KBTV
            2: 232354Z
            3: 31010KT
            4: 1/4SM
            5: +SN
            6: FZFG
            7: BKN003
            8: BKN007
            9: OVC030
    tuckerm@apollo:/mnt/homes/CLASSES/MET4720/LAB04>
    


last updated: 18 Mar 2012 13:15