Sudarshan Raghavan wrote:
> 
> On Fri, 21 Mar 2003, NYIMI Jose (BMB) wrote:
> 
> > > From: Palmer Greg [mailto:[EMAIL PROTECTED]
> > >
> > > $filename = `ls -ltr|tail -1 $DIRECTORY`;
> > > print $filename;
> >
> > Sorry, i didn't mentioned that i wanted a pure perl solution, thanks anyway.
> >
> > my $latest = (sort {-M $a <=> -M $b} <$dir/*>)[0];
> > Will be my way to go, thanks Sudarshan Raghavan
> 
> The above solution will work but a more time effecient way will be to use
> the Schwartzian transform as explained by Rob Hanson in his mail. Thanks
> Rob :-)
> 
> The solution using while that I posted is also more time effecient than my
> sort solution :-)

If you want efficiency then don't use sort at all.  This was about three
to four times faster on my cursory tests then then Rob's example and
about two to three times faster than using `ls -ltr|tail -1` from within
perl.

my $latest;
opendir my $dh, $dir or die "Cannot open $dir: $!";
my $t = ~0;
while ( defined( my $file = readdir $dh ) ) {
  if ( -f "$dir/$file" and  -M _ < $t ) {
    $latest = $file;
    $t = -M _;
    }
  }
print "$latest\n";



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to