#!/usr/bin/perl ################################################ # day_print.pl # Version 1 # Robert D. Cormia # UCSC Programming for Bioinformatics II # October 10, 2003 ################################################ # Write a Perl program called 'day_print' to do the following: # a. Define a hash called 'days' with 'Mon' => 'Monday', ... 'Sun' => # 'Sunday' as the Key-Value pairs # b. Use the function 'localtime' to assign a scalar called $date_time # with the current date. localtime returns a string like this: # Thu Oct 4 11:02:05 2001 # (the Day, Mon, Date, Time, Year elements are separated by 1 space) # c. From this scalar using the 'split' function find out the current day # d. Use this day as a key in the hash 'days' and print the value as # 'Today is a nice Monday or Tuesday or ...' as the case maybe # e. Fetch the year from $date_time and calculate the age of Jon who is # born in 1975. Print it. # use strict; my %day_names; my $date_time; my @date; my $day; my $age; my $year; my $year_born; # Create day_names hash %day_names = ( 'Mon' => 'Monday', 'Tue' => 'Tuesday', 'Wed' => 'Wednesday', 'Thu' => 'Thursday', 'Fri' => 'Friday', 'Sat' => 'Saturday', 'Sun' => 'Sunday' ); # Calculate local time using local_time function $date_time = localtime(); print "Today's local time is $date_time\n"; # Determine day from date_time using split # Create array for date using @day = split(' '$date_time); @date = split(' ', $date_time); # Use first position in the @date array to determine day of week $day = $date[0]; print "Today's abreviated day is $day\n"; # Use abrev. day from $day to access full name of day from %day_names print "Today is a nice $day_names{$day}\n"; # Determine $year from date_time using split # $year is the fifth position in the array @days # # Calculate age of Joe from date_time using split # # $year will equal the last position in the array @date $year = $date[4]; print "Today's year is $year\n"; # Joe was born in 1950. $year_born = '1950' # Year is the fifth element in array @date # Joe's age = $days[4] - $year_born. $year_born = '1950'; $age = $year - $year_born; print "Joe's age is $age years old\n"; __END__