Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: File and pe...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 4 Topic 11014 of 11509
Post > Topic >>

Re: File and perl

by rich.japh@[EMAIL PROTECTED] (Richard Lee) May 1, 2008 at 09:42 PM

Chas. Owens wrote:
> On Thu, May 1, 2008 at 8:45 PM, Richard Lee <rich.japh@[EMAIL PROTECTED]
> wrote:
> snip
>   
>>  ls -ltr | tail -100 | cut -d' ' -f13
>>     
> snip
>
> Let's pick this apart, shall we?
>
> ls -tr gets all of the (non-hidden) files in the current directory
> reverse sorted by time (the l is unnecessary and is why you need the
> cut later) and the tail takes the last one hundred (or fewer) of them.
>
> Well, Perl can get all of the (non-hidden) files in the current
> directory very easily with glob*:
>
> my @[EMAIL PROTECTED]
 = <*>;
>
> The next step is to sort them on mtime (largest mtime values last)
> using stat** to get the mtime and sort*** to sort the list:
>
> my @[EMAIL PROTECTED]
 = sort { (stat $a)[9] <=> (stat $b)[9] } <*>;
>
> All of those calls to stat to get the mtime during the sort can be
> expensive, so we might want to do a Schwartzian Transform**** to speed
> it up:
>
> my @[EMAIL PROTECTED]
 =
>     map { $_->[1] }
>     sort { $a->[0] <=> $b->[0] }
>     map { [(stat)[9], $_] } <*>;
>
> To get the last hundred files we can use a list slice***** using the
> range operator******:
>
> my @[EMAIL PROTECTED]
 = (
>     map { $_->[1] }
>     sort { $a->[0] <=> $b->[0] }
>     map { [(stat)[9], $_] } <*>
> )[-100 .. -1];
>
> But this leaves use with undefs if we have fewer than one hundred
> files in the current directory, so we need a grep******* to weed them
> out:
>
> my @[EMAIL PROTECTED]
 = grep defined, (
>     map { $_->[1] }
>     sort { $a->[0] <=> $b->[0] }
>     map { [( stat)[9], $_ ] } <*>
> )[-100 .. -1];
>
> * http://perldoc.perl.org/functions/glob.html
> ** http://perldoc.perl.org/functions/stat.html
> *** http://perldoc.perl.org/functions/sort.html
> **** http://en.wikipedia.org/wiki/Schwartzian_transform
> ***** http://perldoc.perl.org/perldata.html#Slices
> ****** http://perldoc.perl.org/perlop.html#Range-Operators
> ******* http://perldoc.perl.org/functions/grep.html
>
>   

Thank you and also letting me know on my overlook on using ls (fact that 
I was using unnecessary -l option on it)

Will try them and let you know.

thanks again!
 




 4 Posts in Topic:
File and perl
rich.japh@[EMAIL PROTECTE  2008-05-01 20:45:34 
Re: File and perl
chas.owens@[EMAIL PROTECT  2008-05-01 21:13:15 
Re: File and perl
rich.japh@[EMAIL PROTECTE  2008-05-01 21:42:05 
Re: File and perl
kennethwolcott@[EMAIL PRO  2008-05-02 08:39:23 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 23 21:03:32 CDT 2008.