in reply to How to create the varibles with the file names

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.

Replies are listed 'Best First'.
Re^2: How to create the varibles with the file names
by softworkz (Monk) on Nov 15, 2005 at 21:03 UTC
    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().
Re^2: How to create the varibles with the file names
by duff (Parson) on Nov 15, 2005 at 21:06 UTC

    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.

Re^2: How to create the varibles with the file names
by Anonymous Monk on Nov 15, 2005 at 22:47 UTC
    Thank you so much! It works great!