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!
5 4487
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
Okay, the first link shows me this at the bottom, which i will try: -
## These are illegal and won't even compile:
-
-
@fd = (*STDIN, *STDOUT, *STDERR);
-
print $fd[1] "Type it: "; # WRONG
-
$got = <$fd[0]> # WRONG
-
print $fd[2] "What was that: $got"; # WRONG
-
-
## With print and printf, you get around this by using a block and an expression where you would place the filehandle:
-
-
print { $fd[1] } "funny stuff\n";
-
printf { $fd[1] } "Pity the poor %x.\n", 3_735_928_559;
-
Pity the poor deadbeef.
-
-
## That block is a proper block, so you can put more complicated code there. This sends the message out to one of two places:
-
-
$ok = -x "/bin/cat";
-
print { $ok ? $fd[1] : $fd[2] } "cat stat $ok\n";
-
print { $fd[ 1 + ($ok || 0) ] } "cat stat $ok\n";
-
-
## 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:
-
-
$got = readline($fd[0]);
-
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. =)
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.
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...
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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;...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |