#!/usr/bin/perl -w

# Lab 07 Solution
#----------------------------------------------------------------------
$datafile = $ENV{HOME}.'/work/sunriseset.txt';
#  Opens the file K:/Perl/LAB07/sunriseset.txt for reading.


open(DF, "<$datafile") || die "cannot open file: $!";

# 2. From the data found in the file, creates hashes to store several values 
#  using a date string as the common key. The values should be sunrise time and 
#  sunset time. For example, this should be arranged so that $sunrise{'Jan 04'} 
#  will contain the value "0725". 
<DF>;<DF>; # strip the headers off
while($line = <DF>) {
    chomp($line);
    ($mon, $day, $rise, $set) = split(/\s+/, $line);
    $sunrise{"$mon $day"} = $rise;
    $sunset{"$mon $day"} = $set;
}
close(DF);

# 3. Once the hashes are created, the script will loop prompting the user to 
#    enter a date to display until "q" is entered.
while(1) {  # "1" is "true" in Perl (infinite loop)
    print "Enter a date (ex. Jan 04): ";
    chomp($ans = <STDIN>);
    last if($ans =~ /q/i);
    # 4. Each time the user has provided a date, the script will then print out
    #    the sunrise time and the sunset time for that date using some 
    #    descriptive text.
    print "The sunrise for $ans is $sunrise{$ans}.\n";
    print "The sunset for $ans is $sunset{$ans}.\n";
}

exit;