Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyway to wildcard file input ? 2

Status
Not open for further replies.

ljsmith91

Programmer
May 28, 2003
305
US
I have a PERL script that currently pulls input from a single file. However, now I need to imput several files(I will not know the actual count) and I will not know the actual file name, only that the file will follow a specific file mask.

Today's example code:

$file_input = "input_boston_server.file";
open( FH_INPUT, ">$file_input");
while(chomp($inputline = <FH_INPUT>)) {
...

I now have to read in files that I know will fall under a specific file naming mask but I will not know how many files will be input. File input names can be named:

input_boston_server.file
input_newyork_server.file
input_chicago_server.file
input_dallas_server.file
input_miami_server.file
...etc etc

How can I process these input files if I do not know the full names or the count, I only know the mask. Any ideas ?

Thanks.


 
Something like this should work:
Code:
chdir '/path/to/your/files/';
my @files = <input_*_server.file>;

foreach my $file (@files) {
    # Code to process file
}
The chdir isn't necessary if the script is in the same directory as all the files you're processing or if you don't mind the path included in the file names (ex. @files = </path/to/files/input_*_server.file>;)
 
Looks good to me...I will give that a try. Thanks.
 
maybe use grep() to find the files you will need using a regexp to locate the filenames that match the pattern you require.

Code:
open(DIR,'path/to/directory') or die "$!";
my @list = grep (/input_[a-zA-Z]+_server\.file$/, readdir DIR);
close DIR;

for(@list) {
   my $file_input = $_;
   open( FH_INPUT, ">$file_input");
   while(chomp(my $inputline = <FH_INPUT>)) {
      ...
   }
}

where 'path/to/directory' the the directory these files are in you need to find. But in your sample code you are opening the file for overwriting (>):

$file_input = "input_boston_server.file";
open( FH_INPUT, ">$file_input");
while(chomp($inputline = <FH_INPUT>)) {

so maybe I don't understand your question.
 
oops, race condition! Took me 8 minutes to post that? I'm slowing down in my old age....
 
KevinADC,

The ">" at Open was a boo boo. Your suggestion looks good to. Thanks for the suggestion....it too looks good. Need to put it to practice now. -ljs
 
they are really the same, rharsh used a typeglob(?) <> and I used grep(). Not sure if there is an advantage of one over the other but there might be. Nice of you to give both of us stars :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top