473,387 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do we do an array of File Handles?

I want to open and read a number of files, but this number is an unknown at compile time. So logically i want an array of File Handles to open and read them all, such as FH[0], FH[1], FH[2], ... etc.

How is this done? Are there any examples anywhere? Tia!
Jun 26 '10 #1
5 4487
numberwhun
3,509 Expert Mod 2GB
Not sure if it helps you, but I found this link from the Perl Cookbook, and this from Perlmonks. Both seems to discuss what you are trying to do.

Regards,

Jeff
Jun 26 '10 #2
Okay, the first link shows me this at the bottom, which i will try:
Expand|Select|Wrap|Line Numbers
  1.  ## These are illegal and won't even compile:
  2.  
  3. @fd = (*STDIN, *STDOUT, *STDERR);
  4. print $fd[1] "Type it: ";           # WRONG
  5. $got = <$fd[0]>                     # WRONG
  6. print $fd[2] "What was that: $got"; # WRONG
  7.  
  8.  ## With print and printf, you get around this by using a block and an expression where you would place the filehandle:
  9.  
  10. print  { $fd[1] } "funny stuff\n";
  11. printf { $fd[1] } "Pity the poor %x.\n", 3_735_928_559;
  12. Pity the poor deadbeef.
  13.  
  14.  ## That block is a proper block, so you can put more complicated code there. This sends the message out to one of two places:
  15.  
  16. $ok = -x "/bin/cat";                
  17. print { $ok ? $fd[1] : $fd[2] } "cat stat $ok\n";
  18. print { $fd[ 1 + ($ok || 0) ]  } "cat stat $ok\n";           
  19.  
  20.  ## This approach of treating print and printf like object methods calls doesn't work for the diamond operator, because it's a real operator, not just a function with a comma-less argument. Assuming you've been storing typeglobs in your structure as we did above, you can use the built-in function named readline to read a record just as <FD> does. Given the preceding initialization of @fd, this would work:
  21.  
  22. $got = readline($fd[0]);
  23.  
The next link outlines a few more ways (though convoluted and possibly deprecated code(?), i'm running 5.12.x). Though new to Perl, i'm completely struck by its bend over backwards implementation of simple FH arrays as well as Array of Arrays!

Thanks Jeff for the links for two reasons: one to find the answer(s), and two for even more sites i'll search for help as i need it. =)
Jun 26 '10 #3
eWish
971 Expert 512MB
Why do you need to use an array of filehandles? If you use a lexical filehandle when it goes out of scope you don't have to worry about the things you listed.
Jun 26 '10 #4
I want a indexable random number of open files to read from in Main. e.g.
my @FHA = (); # File Handle Array.
my @filenames = (); # Actual File Names in Array.
...
open($FHA[$i], "<$filenames[$i]",); { do some things;} close ($FHA[$i]);
But the syntax bombs...
Jun 26 '10 #5
RonB
589 Expert Mod 512MB
It's rare to want or need to use an array of filehandles, but I do have one script where I'm storing a few in a hash. In this case I'm running perl 5.8.8.

I ran a few tests on 5.10.1 and it appears that they took out support for using an array or hash for filehandles. You need to use a scalar.
Jun 27 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Woodster | last post by:
I currently have some code for an application that is running on Win32. I have tried to keep anything not directly gui related as separate as possible for portability reasons, including file...
0
by: j jpenney | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, I'm working on a problem we've been having with an OAS deployed application. We have a W2K3 machine running OAS 10g, with...
3
by: gmtonyhoyt | last post by:
Okay, this one's a tough one for me to explain so this might take a few e-mails to get the idea across. Here's what I got though. I have this application running on a Sun/Solaris machine,...
4
by: Tony Liu | last post by:
Hi, how can I create multiple new file handles of a file without having to share to file to the other processes? I have a file that will be accessed by multiple threads in my application, each...
18
by: Conrad F | last post by:
Hello all, I am waiting for receipt of files in a directory. I use the FileSystemWatcher to detect when files arrive in said folder. I need to read the data from these files ASAP but the files...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
1
by: Matt Gertz | last post by:
Phil, Thanks for the notification on this. I'm going to do some investigation on this today & try debugging through the problem, and I'll get back to you either this afternoon or tomorrow...
3
by: mwindham | last post by:
How does one make a gc class that contains an array of handles to an array of X-type? // comments out what I am trying to do: public ref class c0 { private: static System::UInt32 c0_uid;...
2
by: SharpCoderMP | last post by:
hi, in my app i monitor the filesystem for changes with FileSystemWatchers. When the change is detected the app performs some actions using Shell32 to obtain information from the filesystem. now...
1
by: =?Utf-8?B?RGVtZXRyaQ==?= | last post by:
The short question I have here is how would one enumerate file handles for a given process in C#? I know p/invoke is needed. Sysinternals uses NtQuerySystemInformation and NtQueryObject....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.