#!/usr/bin/perl -w

# lab #4 solution

#----------------------------------------------------------------------

$datafile = '/mnt/homes/CLASSES/CIS2279/LAB04/logger.csv';
$outfile = '/mnt/homes/tuckerm/lab04.output';

# Check that the data file "/mnt/homes/CLASSES/CIS2279/LAB04/logger.csv" 
# exists before opening it for read. Exit the script if the file cannot be read.
if( -r $datafile) {
    print "Input file exists, continuing...\n";
}else{
    print "No input file.  Exiting abruptly...\n";
}

# If the file exists, then open the data file and an output file.
open(DF, "<$datafile") || die "cannot open data file: $!";
open(OUT, ">$outfile") || die "cannot open output file: $!";

# Read through the data file using a while loop and check for any data line 
# where the julian date is 068 and the temperature is above 32.
while($line = <DF>) {
    chomp($line);
    @ldata = split(/,/, $line);
    if(($ldata[1] eq '068') && ($ldata[6] > 32)) {
        # Print to the terminal and an output file any line that matches the above criteria.
        print "$line \n";
        print OUT "$line\n";
    }
}

# Close both files.
close(DF);
close(OUT);