Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Return newest file

by cchampion (Curate)
on Aug 13, 2003 at 10:36 UTC ( #283502=note: print w/replies, xml ) Need Help??

in reply to Return newest file

You can get the last file in one go.

$last= (sort { -M $b <=> -M $a } <*.log>)[-1];

If you have a very large number of files, you may consider using the Schwartzian Transform to minimize sorting time.

$last = ( map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_, -M $_ ] } <*.log> )[-1];

Both versions are short enough to be used in a one-liner.

Tested in Linux and Windows. Works fine.

Replies are listed 'Best First'.
Re: Re: Return newest file
by snax (Hermit) on Aug 13, 2003 at 20:29 UTC
    Why torture yourself to grab the last element when you can reverse the sort and grab the first? It strikes me as being semantically clearer as well:
    $newest = (sort { -M $a <=> -M $b } <*.log>)[0];
    Same story with the Schwartz trick.

    Of course, TIMTOWTDI :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://283502]
[Corion]: But who am I to worry about necrocpan - I'm revisiting my online banking module, and also I'll somewhat revive WWW::Mechanize:: Firefox, at least if the test suite passes under the Waterfox browser ...
[Corion]: Maybe I should look through my CPAN releases and revisit them in order of last release and think about what to do with the modules :)
[marto]: this cpan day there should be a call to remove old crud :P
[marto]: as well as encourage active development

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-07-17 08:20 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (359 votes). Check out past polls.