Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: read directory

by shmem (Chancellor)
on Jul 08, 2006 at 10:49 UTC ( [id://559917]=note: print w/replies, xml ) Need Help??


in reply to read directory

You don't. readdir (and glob) return the entries in the order the OS kernel pleases to present them. You have to sort yourself:
@files = sort {-M $a <=> -M $b} <*>; # update - better without -M in sort opendir(D,$dir); @files = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [-M $_, $_] } grep {!/^\.\.?$/} readdir(D);

A related thread is here.

--shmem

update: added ST after reading TedPride's post ;-)

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: read directory
by jbisbee (Pilgrim) on Jul 08, 2006 at 14:12 UTC
    Update: Sorry, I know this post was a bit lame and didn't prove anything

    I just benchmarked the difference between "glob" and "<>" and found very little difference (subsequent runs flip-flopped which was faster). If that's the case, then I'd prefer the glob('pending/*') over the <pending/*> call because of the awkward bare word. But then, thats just me.

    #!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); cmpthese( 20_000, { '<>' => sub { my @results = <pending/*>; }, 'glob' => sub { my @results = glob('pending/*'); }, }, );
    and the results
    Rate glob <> glob 7194/s -- -0% <> 7220/s 0% --

    -biz-

      well, <DATA> also has a bareword.. ;-)

      I'm not surprised that glob and <> down differ much, since they are essentially the same... bigger savings can be achieved by using -M instead of (stat)[9], because doing so eliminates the overhead of making a list just to get its 10th element.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      I just benchmarked the difference between "glob" and "<>" and found very little difference

      That's hardly surprising since glob is the internal function implementing the "<*.c>" operator according to the documentation.

      --
      David Serrano

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://559917]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found