#! /opt/kde/bin/perl -w # start Perl with
verbose warning messages enabled
# Homework Assignment #3 by Thomas Albert
# Nov. 28, 1999 for instructor Jeff Okamoto, U.C. Berkeley Extension
# Introduction to Perl Programming
# This Perl script processes the text file the user specifies on the command line
and validates the data in the file. If the file has invalid data, an error message
notifies the user of the problem.
# read the data file the user specifies on the command line into
a file handle using the @ARGV array
# or, if Perl cannot read the data file, or the user does not specify a data file, display
an error message
open (FH, "@ARGV") || die "This Perl program cannot find a data file named:
@ARGV.\nAt the command line, type the name of program, followed by the name of a valid
data file.\nSystem Error $!\n";
# create an associate array (hash) to store the key/value pairs
of the data file
@stuff = <FH>;
%stuff = @stuff;
# loop through the hash and cut off the line returns
foreach $key (keys (%stuff)) {
$mymonth = $key;
$myday = $stuff{$key};
chomp $mymonth;
chomp $myday;
# check validity of data and calculate the year date
# this program accepts three letter abbreviations and any case for the month names
# and calculates the date for the year by adding the total days of previous months to
# the day of the current month
if (($mymonth =~ /Jan|January/i) && ($myday < 32)) {
$yearday = $myday;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Feb|February/i) && ($myday < 29)) {
$yearday = $myday + 31;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Mar|March/i) && ($myday < 32)) {
$yearday = $myday + 31 + 28;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Apr|April/i) && ($myday < 31)) {
$yearday = $myday + 31 + 28 + 31;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /May|may/i) && ($myday < 31)) {
$yearday = $myday + 31 + 28 + 31 + 30;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Jun|june/i) && ($myday < 32)) {
$yearday = $myday + 31 + 28 + 31 + 30 + 31;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Jul|july/i) && ($myday < 32)) {
$yearday = $myday + 31 + 28 + 31 + 30 + 31 + 30;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Aug|august/i) && ($myday < 32)) {
$yearday = $myday + 31 + 28 + 31 + 30 + 31 + 30 + 31;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Sep|september/i) && ($myday < 31)) {
$yearday = $myday + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Oct|October/i) && ($myday < 32)) {
$yearday = $myday + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Nov|november/i) && ($myday < 31)) {
$yearday = $myday + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
elsif (($mymonth =~ /Dec|december/i) && ($myday < 32)) {
$yearday = $myday + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
print "$mymonth $myday corresponds to the year day: $yearday\n";
}
# Display a message if the data is invalid. Tabs highlight the
non-normative result
else {
print "\tError processing month $mymonth with day $myday\n\tVerify that
your data file has valid month names,\n\tand that the day is within the
range for that month\n";
}
}
# End of this Perl script
# End of this Perl script.