On Mon, 24 Jan 2005 14:31:53 +0100 Jan Eden <[EMAIL PROTECTED]> wrote:
> Hi, > > I create monthly log files with names like statistics_01_2005.log. Now I have > a function which returns the logfile name for the current month: > > sub statfile { > my @date = localtime(time); > my ($month, $year) = @date[4,5]; > $year += 1900; > $month = sprintf ("%02d", $month+1); > return my $statfile = qq{statistics_${month}_${year}.log}; > } > > In some cases I need the name of last month's logfile. To achieve this, I > modified the subroutine like this: > > sub statfile { > my $mode = shift; > my @date = localtime(time); > my ($month, $year) = @date[4,5]; > $year += 1900; > if ($mode) { > $month = sprintf ("%02d", $month); > $month = 12 if $month == 0; > $year = $year - 1; > } > else { > $month = sprintf ("%02d", $month+1); > } > return my $statfile = qq{statistics_${month}_${year}.log}; > } I just get all the file names and sort them, last months is the second last (or first) opendir(DIR,$wherever) or die "Can't open DIR $!\n"; while (defined($file = readdir(DIR))) { next unless($file =~ /\.log/); #just take out the .log files push (@filenames,$file); } closedir(DIR); # Now I get a sorted list for inclusion in a drop down menu @filenames = sort{$a cmp [EMAIL PROTECTED]; Owen -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>