Connecting Tech Pros Worldwide Forums | Help | Site Map

writing to unknown number of files

Newbie
 
Join Date: Jan 2007
Posts: 2
#1: Jan 25 '07
Hi,

I have a problem.

I need to open unknown number of files for writing.
In order to solve the problem I tried to open files in an array,
where each node in the array is a pointer to a file.
I succeed in opening the files and close all the files,
but I can't write to them.

Please help me!

Thanks,
Rotem.

This is the code:
-----------------------------------
open the files (works):
----------------------------------
while ($currFile < $totalFiles)
{
# open data file
open($dataFilesList[$currFile], ">$currFile");
$currFile++;
}
--------------------------------------------------
write to the files (doesn't work):
--------------------------------------------------
while ($currFile < $totalFiles)
{
print $dataFilesList[$currFile] ("bla bla");
$currFile++;
}

This is the error:
Not a CODE reference at ./script.pl line 263, <INPUT_FILE> line 1

------------------------------------
close the files (works):
------------------------------------
while ($currFile < $totalFiles)
{
# close data file
close($dataFilesList[$currFile])
$currFile++;
}

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Jan 25 '07

re: writing to unknown number of files


Look into using the IO::File module for this task.
docsnyder's Avatar
Member
 
Join Date: Dec 2006
Location: Darmstadt
Posts: 88
#3: Jan 26 '07

re: writing to unknown number of files


you are using $currFile and $totalFiles as numbers:
Expand|Select|Wrap|Line Numbers
  1. while ($currFile < $totalFiles)
  2. ...
  3. $currFile++
  4. ...
But, when opening files
Expand|Select|Wrap|Line Numbers
  1. open($dataFilesList[$currFile], ">$currFile");
you use $currFile as a filename.

This can't work.

Greetz, Doc
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Jan 26 '07

re: writing to unknown number of files


Quote:

Originally Posted by docsnyder

you are using $currFile and $totalFiles as numbers:


you use $currFile as a filename.

This can't work.

Greetz, Doc

It could work, $currFile is just the index number of the array element $dataFilesList[$currFile]. If the array element were a proper filehandle then it could work. It's just not necessary to do it that way.
Newbie
 
Join Date: Jan 2007
Posts: 2
#5: Jan 28 '07

re: writing to unknown number of files


Hi All,
Thanks for your suggestions.
This was the solution I found:

while ($currFile < $totalFiles)
{
$file=$dataFilesList[$currFile];
print $file ("bla bla");
$currFile++;
}

Regards,
Rotem.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#6: Jan 29 '07

re: writing to unknown number of files


this doesn't work?

Expand|Select|Wrap|Line Numbers
  1. while ($currFile < $totalFiles)
  2. {
  3. print $dataFilesList[$currFile] ("bla bla");
  4. $currFile++;
  5. }
Reply