Gossamer Forum
Home : General : Perl Programming :

Sorting...

Quote Reply
Sorting...
I've tried this every which way but loose. I made this progam:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<html>\n\n";

opendir (DOPPLER, "/home/gennus/gennus-www/wfmy/doppler/");
@filenames = grep (!/^capture/ && /-/, readdir (DOPPLER));
closedir (DOPPLER);
@filenames = sort { $a <=> $b } @filenames;
unlink "/home/gennus/gennus-www/wfmy/doppler/$filenames[0]";
exit;

What it does is opens the directory, sorts the files from oldest to newest and deletes the oldest. This all works fine from my browser. BUT, I have a program that runs it every five minutes from a remote machine by calling the URL. Much like a browser, I would think. Anyway, when I run it from this program, evidently it sorts them wrong because it deletes the newest one.

Help!


------------------
WFMY-TV Webmaster
Quote Reply
Re: Sorting... In reply to
Hi,

@filenames = sort { $a <=> $b } @filenames;

That doesn't look right. What you end up with is a sorted list of files based on how their filenames sort numerically.

How about:

$root = "/home/gennus/gennus-www/wfmy/doppler/";
@filenames = sort { -M "$root/$a" <=> -M "$root/$b" } @filenames;

-M will return the number of days since the file was last modified/created.

Cheers,

Alex
Quote Reply
Re: Sorting... In reply to
My file names are numeric and I finally got this to work.

opendir(DOPPLER, "/path/") &#0124; &#0124; die "Cannot open directory";
@filenames=sort(grep(!/^capture/ && /-/, readdir (DOPPLER)));
closedir (DOPPLER);
unlink "path/$filenames[0]";
exit;

I can sure use your code for future reference!

Thanks Alex!

------------------
WFMY-TV Webmaster