This program needs to be automated to the point of taking a given folder (stated by user), and assigning a filehandle to each text file. Once this is done, I need to be able to reference each so my basic parsing element can act on each file and converge the data into one output file.
If I read that right, you want to have all of the filehandles at the same time, presumably so you can read from each as you need to rather than process the files sequentially. This mostly untested code:
will return a reference to an array of filehandles open for reading. (You must pass in the path to the directory where the files are.) Keep in mind that you may have limits on the number of filehandles you can have open at once.sub get_filehandles { my $dir = shift; opendir my ($dh), $dir; my @fhs = map { my $fh; open( $fh, '<', "$dir/$_" ) ? $fh : do { warn "$dir/$_: $!"; () } } grep { -f "$dir/$_" } readdir($dh); closedir $dh; return \@fhs; }
Update: Fixed a bug. I was always returning the filehandle whether it was successfully opened or not. It shouldn't barf with warnings on now. I also tried to improve its readability a bit.
-sauoq "My two cents aren't worth a dime.";
In reply to Re: Variable numbers of folders...
by sauoq
in thread Variable numbers of folders...
by bioinformatics
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |