CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
sort dir

 



ceo
New User

Oct 20, 2003, 12:28 PM

Post #1 of 4 (581 views)
sort dir Can't Post

Hi,

I want to know how to return (via SSI/print) the most recent file in a dir.
When a new clients sign up I put them in a new dir and then display the new users name using the print command.

Here is what I have at present:


Code
  #!/usr/bin/perl    

#-#############################################


$basepath = '/home/----/---------/'; erased for obvious reasons.....
$newmember = 'nm';
print "Content-type: text/html\n\n";
opendir THEDIR, "$basepath$newmember" || die "Unable to open directory: $!";
@allfiles = readdir THEDIR;
closedir THEDIR;
foreach $file (sort { int($a) <=> int($b) } @allfiles) {
if (-T "$basepath$newmember/$file") {
open THEFILE, "$basepath$newmember/$file";
($alias) = <THEFILE>;
close THEFILE;
chomp($alias);
}
}
print <font face=\"verdana,Arial\" color=\"black\" size=\"1\">AuctionEnid welcomes it's newest member<br><b>$alias</b></font>";
#------------------------------------------------------#



What it is doing is not returning the latest file in the dir (nm)

I have a file named Loop_y (entered last week) and one called Ebe01 (today) and it's returning the Loop_y file.


...this seems to be sorting the directory listing (files) in alphabetical order.

How can I get it to sort the dir listing by date...latest one first?



many thanks

CEO


KevinR
Veteran


Oct 20, 2003, 9:20 PM

Post #2 of 4 (577 views)
Re: [ceo] sort dir [In reply to] Can't Post

what you are wanting to do and what you posted don't seem too related. That looks like everyauction (or a clone thereof) code, why not ask on the everyauction website? I am sure there is already an addon to do exactly this type of thing. Maybe search the everyauction addon database.

http://209.15.166.219/cgi-bin/codepost.pl?action=listcategories&product=EveryAuction+1.5x
-------------------------------------------------


davorg
Thaumaturge / Moderator

Oct 21, 2003, 2:14 AM

Post #3 of 4 (576 views)
Re: [ceo] sort dir [In reply to] Can't Post

There are a couple of strange things going on in that code.

1) What do you think that int($a) will return when $a contains a filename? It will almost certainly return 0 (unless your filenames start with digits) and therefore the file ordering won't be changed at all.

2) You seem to be reading the contents of every file. I thought you wanted the contents of the most recent file. Why waste processor time opening all of the others?

Here's how I'd do it.

Code
my $path = '/path/to/your/new/member/files'; 
opendir NEW, $path or die $!;

my @files = sort { -M "$path/$a" <=> -M "path/$b" } readdir NEW;
closedir NEW;

my $latest = $files[-1];

open MEMBER, "$path/$latest" or die $!;
my $alias = <MEMBER>;
close MEMBER;
chomp $alias;


The most recent name is now in $alias.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ceo
New User

Oct 21, 2003, 1:48 PM

Post #4 of 4 (570 views)
Re: [davorg] sort dir [In reply to] Can't Post

thanks a bunch..works like a dream Laugh

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives