tc1364 has asked for the wisdom of the Perl Monks concerning the following question:

My question is the sort command appears to be failing when comparing 11-10-2004, 11-09-2004, 11-08-2004, 11-07-2004. The code should be removing the 11-07-2004 directory. The testing that was performed prior appeared to work fine when comparing directories that all had 11-0X-2004. Where did I go astray?
sub dirname () { @date = (); $dirname = ""; @date = localtime(); $dirname = sprintf("%02d-%02d-%04d", $date[4]+1, $date[3], $date[5 +]+1900, @d ate); @dir = (); opendir(DIR, "$path") or die "LOG->Can't open $path: $!\n"; while (defined($file = readdir(DIR))) { if ($file =~ /^\.\.?$/) { next; } if ($file =~ /^\d{2}-\d{2}-\d{4}$/ && -d $file) { push(@dir, "$file"); } } close(DIR); $records = 0; $records = $#dir + 1; if ($records >= 5) { print LOG "Program Error 8 - @dir\n"; exit 1; } if ($records == 4) { foreach $item (sort { $b <=> $a } @dir) { rmtree("$path/$item") or die "LOG->Error is: $!\n"; last; } } if (! -d "$path/$dirname") { mkdir("$path/$dirname", 0777) or die "LOG->Error is: $!\n"; } }

Edit by castaway - fixed closing code tag

Replies are listed 'Best First'.
Re: Removing youngest directory
by steves (Curate) on Nov 09, 2004 at 21:05 UTC

    Your specific issue is that you're using the <=> operator to compare non-numeric values. Try using cmp instead. Test program to illustrate:

    use strict; my @dirs = (qw/11-08-2004 11-10-2004 11-07-2004 11-09-2004/); print "Using <=>:\n", join("\n", sort {$b <=> $a} @dirs), "\n\n"; print "Using cmp:\n", join("\n", sort {$b cmp $a} @dirs), "\n";
    produces:
    Using <=>: 11-09-2004 11-07-2004 11-10-2004 11-08-2004 Using cmp: 11-10-2004 11-09-2004 11-08-2004 11-07-2004

    Your directories will lexically sort as is, but they are not numbers since they have dashes.

      Thank you... the cmp is much better. As far as the quotes, I will need to remember this. the reason why i have been doing this is because this was the way i was taught.
Re: Removing youngest directory
by ikegami (Patriarch) on Nov 09, 2004 at 22:00 UTC
    11-10-2004, 11-09-2004, 11-08-2004, 11-07-2004. The code should be removing the 11-07-2004 directory.

    Your post is titled "Removing youngest directory", but 11-07-2004 would be the oldest, not the youngest.

    Also keep in mind that your program will fail when the year changes because you are sorting by month-day-year instead of year-month-day.

      thank you for all of the pointers, i sincerely appreciated the time you have taken to help me. sorry about not repling sooner but I have been helping troubleshoot someone elses code. Terry.
Re: Removing youngest directory
by trammell (Priest) on Nov 09, 2004 at 20:45 UTC
    A couple of comments on your code:
    • Using a date format of YYYYMMDD is much cleaner, since lexical, numeric, and chronological sorts are identical.
    • use strict;
    • No need to use function prototypes.
    • I see a lot of overuse of quotes in your code, e.g. push(@dir, "$file") is better written push(@dir, $file)
Re: Removing youngest directory
by diotalevi (Canon) on Nov 09, 2004 at 21:32 UTC
    This is an unrelated issue but the name dirname() is completely wrong for something that modifies things. Your function names should tell you what they do. This should have been called remove_newest_directory() or something.