Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: filtering file names to be added to an array...maybe.

by myocom (Deacon)
on Jun 06, 2001 at 09:06 UTC ( [id://86121]=note: print w/replies, xml ) Need Help??


in reply to filtering file names to be added to an array...maybe.

You're on the right track. Rather than using the readdir bit, try using glob, as follows:

my @files = glob('*.dat');

Excellent job with the -w on the #! line! You should also add use strict; just under that. This will force you to declare your variables, among other things, so that's why my code snippet above uses my.

Chatterbox-inspired clarification: You can feed glob a path as part of its argument, like my @files = glob('/temp/*.dat'); Otherwise, glob acts on the current working directory.

Replies are listed 'Best First'.
Re: Re: filtering file names to be added to an array...maybe.
by Coyote (Deacon) on Jun 06, 2001 at 09:26 UTC
    Doesn't glob use the system shell to grab a file list? The OP mentioned that the script needs to run on both NT and *nix. I'm not sure that it would cause any problems in this case, but it could be a potential portability issue.

    ----
    Coyote

      Up to perl5.005_03, glob in Perl spawned a shell. However, as of 5.6.0, glob is implemented using the File::Glob extension (offsite link), which doesn't spawn a shell, and provides configuration options besides.

      As the docs say, it globs like /usr/sh would. And I tested this sort of glob on Win32 and it worked just fine.

      Update: Excellent point, Coyote.

        There is a small issue with glob between Win32 and *nix. Consider a directory that contains files named "uc.DAT" and "lc.dat". Let's use this code to glob all files with an extension of dat on a linux box and a Win98 box (I don't have an NT box handy so YMMV).

        #!/usr/bin/perl -w use strict; use Data::Dumper; my @files = glob('*.dat'); print Dumper(\@files);
        Running this on linux yields this output:

        $VAR1 = [ 'lc.dat' ];
        On Windows 98, you get the following:

        $VAR1 = [ 'lc.dat', 'uc.DAT' ];

        The problem here is that Windows is case insensitive, but case preserving. This could be a potential problem if you are expecting the same output from glob on both platforms.

        ----
        Coyote

      If the C shell exists then Perl will use it to do filename globbing. The benefit of using the C shell is that it properly handles filenames with spaces in them, whereas some other shells do not.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found