|Don't ask to ask, just ask|
Open multiple file handles?by onlyIDleft (Beadle)
|on May 07, 2011 at 02:50 UTC||Need Help??|
onlyIDleft has asked for the
wisdom of the Perl Monks concerning the following question:
Hi all, can anyone help me with how to input multiple files?
Essentially, I want my script to open and close as many file handles as INPUT as the number of ARGV elements from command line, but without a priori knowledge of this number.
Basically I have a list of names and their number of occurrences in each input file.I then need to make a union of all these lists, and then compare this BIG list to each of the smaller lists and output a matrix of this comparison.
For the reason of needing to make these comparisons I cannot open and close the filehandle for INPUT in a while loop, because I can concatenate only after reading in ALL input files!
Once again, my input could very well be 500 files or 2 files, or 10000 files...How do I OPEN and CLOSE as many file handles as the number of arguments at command line without a priori knowledge of this?
For example, if I have 3 input files as follows:
FILE 1a 1 b 2 c 3
FILE 2a 5 d 3
FILE 3a 1 x 4
Then my final output should look like
NAME FILE1 FILE2 FILE3
a 1 5 1
b 2 0 0
c 3 0 0
d 0 3 0
x 0 0 4
Thanks and have a great weekend.
Thanks to you for all your suggestions. I've started teaching myself PERL since only 3 weeks, and had never come across the need to process multiple input files, hence my continuing confusion. Based on your responses, am I right in understanding the following?
1. I do NOT have to open as many file handles as the number of ARGV elements from command line? Just one file handle is sufficient, and to use it in a while loop to point to each of the input files in turn, yeah?
2. I can build a merge of the lists in each input file into a NEW list held in memory and not an actual list or file that is being written into, correct? This seemed intuitive even to me, but am I right?
3. It is possible to compare this merge list in memory to the individual file lists from input? <\p>
Except I still DO NOT understand how to do this comparison between MERGE list in memory versus individual lists form each input file, especially when opening multiple files with ONLY 1 common file handle!
I am a little lost here because of this...My understanding is that the final merge list can be compiled only after reading the contents of the last input file, at which time the common file handle is pointing to the last file. So how can a comparison of the merge list to ANYTHING but the last file be made?
I know I am being naive, but I am sure Your Holiness' the Monks will be kind to a new initiate :)
Thanks to all those who wrote down partial and even FULL scripts, it is very sweet and kind of you. Since I am learning and very much a newbie, I think I would appreciate it and perhaps benefit much more if you could point out an outline of your algorithm and refer me to the operations/syntax that I should teach myself, so that I may implement the most suitable algorithm for my work
Also, my example files and file names have been misleading: my input files are not numbered, they have unrelated alphabetical names. And each of the files is not an array, it is more in the format of a 2 column Excel sheet, but as txt file. Entries in each line separated by tab, lines themselves separated by newline
Thanks again, I aspire to be as helpful and patient as all of you, one day! :)