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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question on FileHandles

Status
Not open for further replies.

BStopp

Programmer
Dec 29, 2003
29
US
Is it possible to create an array of file handles?.. like so:

Code:
@files = ("file1", "file2" ,"file3");
@fhs = new FileHandle;

for( $i = 0; $i <= @files; $i++)
{
   open($fhs[$i], "<$files[$i]");
}

This complies correctly.. and even runs. But when we try to go through each file and pattern match a line for data retrival, it doesn't find the line even when matching a literal in the code to a literal in the file.

Anyone able to give us some insight on this?
 
Code:
use FileHandle;

@files = ("file1","file2","file3");
$fh = new FileHandle;
foreach $file(@files){
    if ($fh->open ("<$file")){
        while (<$fh>) {
            print if /text to match/;
        }
        $fh->close;
    }
}
 
Actually, you missed the point of the question. The crux of our problem is as follows:

We need to open 12 different files at the same time, sort through them and gather data in specific locations in each.
Since each file has the same structure, we want to do it in a loop.

What we ended up doing was this:

Code:
@files = a list of file names to open in a specific order
@header = a list of headers to find in each file
@fh = an array of file handles

for ($i = 0; $i < @files; $i++)
{
   $file = $fh[$i];
   while (<$file>)
   {
      if $_ = /$header[$i]/
      {
         Do some stuff;
      }     
   }
}

What we don't get, why does assigning individual position of the array to another variable ($file) work for the while loop when just referencing it as a the array position ($fh[$i]) not work?
 
instead of the for loop, use

foreach (@fh)

this will look through the files specified in order
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top