Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need use the file names to do something. Example: I have some files in /data. I only need the files that have extension '.txt'. How do I store it as varible that I can use it later? Thanks!
  • Comment on How to create the varibles with the file names

Replies are listed 'Best First'.
Re: How to create the varibles with the file names
by Roy Johnson (Monsignor) on Nov 15, 2005 at 19:37 UTC
    See glob.
    my @txt_files = glob('/data/*.txt');

    Caution: Contents may have been coded under pressure.
Re: How to create the varibles with the file names
by tirwhan (Abbot) on Nov 15, 2005 at 19:40 UTC

    I assume this is what you want:

    @filenames=</data/*.txt>

    Just a note, simple questions like this are probably better asked in the Chatterbox (of course you need to log in for that).


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: How to create the varibles with the file names
by Ovid (Cardinal) on Nov 15, 2005 at 19:39 UTC

    Use a hash.

    my %data_for; # XXX bad name, but I don't know what you need foreach my $filename (@filenames) { next unless $filename =~ /^(.*)\.txt$/; $data_for{$1} = 'whatever you need to store'; }

    Cheers,
    Ovid

    New address of my CGI Course.

Re: How to create the varibles with the file names
by ickyb0d (Monk) on Nov 15, 2005 at 19:45 UTC
    might go something like...
    my @files; #reading directory opendir(MYDIR, "data"); my @contents = readdir MYDIR; #going through all files in directory foreach my $file(@contents) { #if file is a .txt if($file =~ m/\.txt$/) { #store file to array push(@files, $file); } } #closing directory handle closedir MYDIR;
    now all of your .txt files should be stored in the @files array.
      I'd use File::Find

      #!/usr/bin/perl -w use strict; use File::Find; my $dir = "C:\\data\\"; find(\&Do_Something, $dir); sub Do_Something{ if ( -f && /\.txt$/ ) { print "Doing Something $_ \n"; } }

        Except that File::Find recurses into subdirectories and the OP didn't indicate that recursion was needed.

        Also, there's more conceptual baggage with File::Find. If I'm going to gather the files into an array, how do I do that? How do I not recurse if that's appropriate? Et cetera. It's alot more for someone to have to figure out over a simple glob or readdir

        Well, if you're going to use File::Find, you might as well use the saner syntax of File::Find::Rule.
        #!/usr/bin/perl use warnings; use strict; use File::Find::Rule; my $dir = "C:/Windows"; my @files = File::Find::Rule ->file() ->name('*.txt') ->mindepth(1) ->maxdepth(1) ->in($dir); print "$_\n" for @files;
        For a job this simple, though, I would probably use glob().

      I always find it strange that people loop and push when often it's map or grep that's needed.

      opendir(MYDIR,"dir") or die "$!\n"; @txtfiles = grep { /\.txt$/ } readdir MYDIR; closedir MYDIR;

      Granted, your exposition was annotated for learning purposes.

      Thank you so much! It works great!