SPUG: Sorting Files by Mod. Times

Tim Maher/CONSULTIX tim at consultix-inc.com
Wed Oct 6 11:37:33 CDT 1999


>> Message submitted at: Wed Oct  6 09:37:33 PDT 1999
X-Mailer: ELM [version 2.4 PL25]
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Length: 1272      

> ## print links
> 	open (LINKS,"$data_path/$item") or die print"Can't open $data_path/$item";
>     my @links = <LINKS>;
> 	close (LINKS);
> @links = sort {$a cmp $b}@links;
> 	foreach my $link (@links){
> my $reallink = $link;
> $reallink=~tr/ /+/;
> 			(my $name,my $url,my $description)= split(/\|\|\|/,$link);
> 			print qq!
> 
> What this is doing is sorting results alphabetically from the directory
> instead of by newest entry. I was hoping you could help me with this I have
> been pulling my hair out over it.

You asked for alphabetic sorting when you said "sort {$a cmp $b}" ;
I think what you want is "sort  { (stat $a)[9] <=> (stat $b)[9]; }", 
so you can compare the files by their modification times (returned
by the stat function).

WARNING:  I just made this up, and didn't test it!

> 
> Thanks
> Dan Hildreth
> danhil at usinternet.com
> 

*==================================================================*
| Tim Maher, PhD  CEO, Consultix &    (206) 781-UNIX/8649          |
|      Pacific Software Gurus, Inc.   Email: tim at consultix-inc.com |
| "The UNIX/Perl Training Experts"    http://www.consultix-inc.com |
|Classes: 11/1 Shell/Utils  11/15 Adv Shell  12/7 LINUX  12/13 Perl|
*==================================================================*

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list