Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

using File::Find for reading files in order

by Ashes.cfg (Initiate)
on Feb 04, 2009 at 06:44 UTC ( #741195=perlquestion: print w/ replies, xml ) Need Help??
Ashes.cfg has asked for the wisdom of the Perl Monks concerning the following question:

hello All, Its a long time I have worked on Perl(an yr).I have a simple question. I am using File::Find module to traverse through my directory and access my file
sub messages { my $dir = shift or return; my @messages; find(sub { next if /^\.+$/ || -d $File::Find::name; push @messages, $ +File::Find::name }, $dir); return @messages; }
The Output I get is really strange..Consider I have 100 files(numbered 1 thru 100)...somehow the way the files are accessed is
File 1 File 10 File 100 File 2 File 20 File 21 . . and so on
I think I am making a mistake in the regex.Holy Monks...Kindly help me.... The Rest of my code is working correctly. Regards Ashes

Comment on using File::Find for reading files in order
Select or Download Code
Re: using File::Find for reading files in order
by ysth (Canon) on Feb 04, 2009 at 06:50 UTC
      Tht worked wonders.. Thanks a lot ysth. I think I would be more on perlmonks in coming months, since I might have to script a lot in coming weeks

      Either that, or if there are many files that will be skipped by the regexp, sort the results just before returning them.

      return sort {$a <=> $b} @messages;

      • another intruder with the mooring in the heart of the Perl

Re: using File::Find for reading files in order
by jwkrahn (Monsignor) on Feb 04, 2009 at 10:23 UTC

    If you had warnings enabled then you would have received the message Exiting subroutine via next at YOUR_PROGRAM line N.

    You need to use return to exit from a subroutine instead.

Re: using File::Find for reading files in order
by hbm (Hermit) on Feb 04, 2009 at 13:50 UTC

    Another tip: In the find sub, $_ is equivalent to File::Find::name, so you could do:

    find sub { push @messages, $_ if (-d $_ && !/^\.+$/); }, $dir
      In the find sub, $_ is equivalent to File::Find::name
      Nope. $_ holds the name relative to the current directory at the time of the subroutine call, which in the default case has been chdir'ed to the location of the file. But $File::Find::name is always relative to the original directory.

      So they aren't the same. Definitely different uses. You can use $_ inside the subroutine, but if you want to save the name for later, you want to use $File::Find::name. And in fact, it'd be wrong to use $File::Find::name within the subroutine, because your relative name would have been applied twice. Ouch.

        merlyn, Thanks for clarifying. I was hesitant to say "equivalent"; even to say anything other than "you can shorten your code..." And now I've reread the doc, a Good Thing.

        I've reread the OP too, and I think the claimed output is misleading. Surely it is more like the following? (In which case my suggestion can be stricken, as it would only yield the files.)

        /path/to/File 1 /path/to/File 10 /path/to/File 100 /path/to/File 2 /path/to/File 20 ...

        Whew.

Re: using File::Find for reading files in order
by leocharre (Priest) on Feb 04, 2009 at 14:37 UTC
    I'm just curious here.. Why do you think
    File 1
    File 10
    File 100
    File 2
    File 20
    File 21
    Is out of order? Or strange at all?

    It's obviously sorted alphanum.
    You expected to sort by comparison, by.. is two greater than, 100. It's not greater ammount, but 100 does come before 2 alphanumerically. Just wanted to point that out- because this is a bug in the coder, not the code.

    If you want to be able to sort files like these as you expect, one thing to do is name them appropriately...

    # rename ' ' ' 00' File\ ?; rename ' ' ' 0' File\ ??
    # ls -1 File\ *
    File 001
    File 002
    File 010
    File 020
    File 021
    File 100
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://741195]
Approved by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-09-17 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (79 votes), past polls