So I am reading through a calendar file to insert a date into the file and I want the dates to remain in chronoligical order. The problem comes when I find the place where the date should go, the file is already looking past the point I want to insert into.
The calendar file I'm looking at looks like this:
# November 2010
11/26/2010
11/27/2010
11/28/2010
11/29/2010
11/30/2010
# December
12/24/2010
12/25/2010
12/26/2010
12/27/2010
12/28/2010
12/29/2010
12/30/2010
and my code looks like this:
while (my $line = <FILE>) {
if (substr($line, 0, 1) =~ m/\#/ || $line =~ m/calendar/) { #if the line is commented out or contains the calendar's name skip to next line
next;
}
chomp($line);
my ($temp_month, $temp_day, $temp_year) = split(/\//, $line, 3);
if ($year == $temp_year && $month == $temp_month && $day < $temp_day) {
?
}
}
So are there any suggestions as to how to point to the previous spot in the file?
The function you need to move randomly around a file is seek. But there's more information on how to approach this problem in the Perl FAQ - How do I change, delete, or insert a line in a file, or append to the beginning of a file?