Perl Examples by Thomas Albert


#! /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


#! C:\local\Perl5\
# Thomas Albert (23 August 1999)
# talbert@rational.com
# assignment for Susan Barr, UCSC Extension Instructor
# http://www.hooked.net/~sbarr/
# Perl for Novice Programmers, Santa Clara, 10-13 August 1999, EDP 991K58

# This script enables a user to fetch information
# about Academy Award Winning Films
# by typing any string the film data file (acawd) contains.



# Open the file and store it in an array.
# If the file doesn't open, inform the user with the file path and script
# line number.

open (FH, "<acawd") || die ("Sorry, this program cannot open the input
file");
@myarray =<FH;

# Get the string pattern the user wants to retrieve from the file.
print ("Enter a pattern to search for:\n");
$pattern = <STDIN;
chop ($pattern);

# Reverse the array so most recent movies appear first.
@sorted = reverse (sort (@myarray));

# Initialize a counter to track the number of matching lines.
$count = 0;

# Loop through the file to find matches and format them.
foreach $i (@sorted) {
   if (($i =~ m/$pattern/i))
   {
# print the here are your results message only once.
      if ($count == 0) {
      print "Here are your results\n----------------------\n"
      };
      # Increment the counter to track the number of matching lines.
      ++$count;
      @data = split(/\#/g,$i);
      $year = "Year: ".shift(@data)."\n";
      $rank = "Number: ".shift(@data)."\n";
      $title = "Title: ".shift(@data)."\n";
      $director = "Director:".shift(@data)."\n";
      @data = ($year, $rank, $title, $director);
      # print results to the search as we loop.
      print "$year$rank$title$director";
   }
}
print "You had $count match(es)\n";
# If the string does not exist in the data file, inform the user.
   if ($count == 0) {
      print "Here are your results\n----------------------\nSorry, nothing
      matches $pattern.\n";
}

# End of this Perl script.