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 4 of 4 Topic 11014 of 11531
Post > Topic >>

Re: File and perl

by kennethwolcott@[EMAIL PROTECTED] (Kenneth Wolcott) May 2, 2008 at 08:39 AM

------=_Part_6742_19664599.1209742763852
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Now *THAT* is helpful! Descriptive yet succinct w/ references. Methodical.
Awesome.

--Ken Wolcott

On Thu, May 1, 2008 at 6:13 PM, Chas. Owens <chas.owens@[EMAIL PROTECTED]
> 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
>
> --
> Chas. Owens
> wonkden.net
> The most im****tant skill a programmer can have is the ability to read.
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@[EMAIL PROTECTED]
> For additional commands, e-mail: beginners-help@[EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>

------=_Part_6742_19664599.1209742763852--
 




 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 Sat Jul 26 1:07:22 CDT 2008.