Re: Unix ls -lrt | tail -1 in Perl

2003-03-21 Thread John W. Krahn
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]



RE: Unix ls -lrt | tail -1 in Perl

2003-03-21 Thread Sudarshan Raghavan
On Fri, 21 Mar 2003, NYIMI Jose (BMB) wrote:

> > -Original Message-
> > From: Palmer Greg [mailto:[EMAIL PROTECTED] 
> > Sent: Thursday, March 20, 2003 4:50 PM
> > To: NYIMI Jose (BMB)
> > Subject: RE: Unix ls -lrt | tail -1 in Perl
> > 
> > 
> > $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
> 
> José.

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 :-)

hth



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



RE: Unix ls -lrt | tail -1 in Perl

2003-03-21 Thread NYIMI Jose (BMB)
> -Original Message-
> From: Palmer Greg [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, March 20, 2003 4:50 PM
> To: NYIMI Jose (BMB)
> Subject: RE: Unix ls -lrt | tail -1 in Perl
> 
> 
> $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

José.



 DISCLAIMER 

"This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


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



RE: Unix ls -lrt | tail -1 in Perl

2003-03-20 Thread Hanson, Rob
If you use the first version that Sudarshan posted, you might want to use
this instead.  It uses map, and is a lot faster.

my $latest = (sort {$b->{mtime} <=> $a->{mtime}}
 map {{mtime => -M $_, file => $_}}
 <$dir/*>)[-1];

print $latest->{file}, "\n";

Benchmark results with and without using map:

Benchmark: timing 100 iterations of No-MAP, With-MAP...
No-MAP: 13 wallclock secs ( 2.43 usr  6.43 sys +  4.00 cusr  1.41 csys =
0.00 CPU)
  With-MAP:  8 wallclock secs ( 1.57 usr  0.81 sys +  4.04 cusr  1.27 csys =
0.00 CPU)

Using map allows -M to be performed on each file only once (instead of once
per sort iteration).  And since this is an expensive operation doing it only
once will save a lot of CPU time.

Rob


-Original Message-
From: Sudarshan Raghavan [mailto:[EMAIL PROTECTED]
Sent: Thursday, March 20, 2003 9:17 AM
To: Perl beginners
Subject: Re: Unix ls -lrt | tail -1 in Perl


On Thu, 20 Mar 2003, NYIMI Jose (BMB) wrote:

> Hello,
> 
> How can I do this unix command  in Perl ?  : ls -lrt | tail -1
> Actually, i would like to get the most recent file from a given directory.

my $latest = (sort {-M $b <=> -M $a} <$dir/*>)[-1];

or

my $latest;
while (<$dir/*>) {
$latest = (defined ($latest) && (-M $_ > -M $latest)) ? $latest : $_;
}

Remember to check for the definedness of $latest if $dir is an empty 
directory

> 
> Thanks in advance for your input.
> 
> José.


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

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



Re: Unix ls -lrt | tail -1 in Perl

2003-03-20 Thread Sudarshan Raghavan
On Thu, 20 Mar 2003, NYIMI Jose (BMB) wrote:

> Hello,
> 
> How can I do this unix command  in Perl ?  : ls -lrt | tail -1
> Actually, i would like to get the most recent file from a given directory.

my $latest = (sort {-M $b <=> -M $a} <$dir/*>)[-1];

or

my $latest;
while (<$dir/*>) {
$latest = (defined ($latest) && (-M $_ > -M $latest)) ? $latest : $_;
}

Remember to check for the definedness of $latest if $dir is an empty 
directory

> 
> Thanks in advance for your input.
> 
> José.


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