Question
Write a PERL program that opens and reads the calendar file. Then the program writes back in the calendar file the original content, sorted in
Write a PERL program that opens and reads the calendar file. Then the program writes back in the calendar file the original content, sorted in ascending order by date.
The main code should be 7 lines of code, or less:
open the file for input
read all the input data
close the file
sort the dates
open the file for output
print the result to the file
close the file
Use the sort function. In addition to the main code, you will need to code a sort subroutine that compares the dates of two records. This subroutine should be called by the sort function.
I have the below code, but i am getting the error - readline() on closed filehandle $fh at problemE3.pl line 2.
open (my $fh, "<", "calendar");
push(@events, $_) while(<$fh>);
close($fh);
my @sortedevents = sort dateSort @events;
open(my $oh, ">", "sortedCalendar");
print $oh @sortedevents;
close($oh);
sub dateSort
{
$a =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/;
$a =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/;
$b =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/;
my @date2 = ($1, $2, $3);
$date1[2] <=> $date2[2] || $date1[0] <=> $date2[0] || $date1[1] <=> $date2[1];
}
CALENDER FILE.txt
11/17/1999 - Doctor's appointment.
12/6/1999 - Dentist appointment.
12/23/1999 - Christmas party.
1/23/2000 - Meet with business advisor.
1/13/2000 - Business trip to Hong Kong.
2/25/2000 - Business trip to Kansas City.
1/5/2000 - Car service.
3/18/2000 - Birthday party.
4/10/2000 - Meet with tax accountant.
3/12/2000 - Conference in San Jose.
12/31/1999 - New Year's party.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started