I think I would do this using IO::File from the outset and would store information in a ref. to a HoH keyed by filename rather than in an array. I would start with a list of files to be processed in an array and would populate the hash with "handle" and "buffer" key/value pairs accessed via subroutines to keep it all in step. This way I have the advantage (if needed) to work with files both by name and by their index in a list of files.

In the script below I am reading four data files, spw592341.dataA to D, each containing a few lines that can easily be identified. Here's the script

use strict; use warnings; use Data::Dumper; use IO::File; print qq{Files to read\n}; my @dataFiles = glob q{spw592341.data*}; print qq{ $_\n} for @dataFiles; print qq{\n}; my $rhFiles = {}; foreach my $file (@dataFiles) { openFile($file); getLine($file); } showData(q{After reading first line}); foreach my $file (@dataFiles) { getLine($file); } showData(q{All files read again}); getLine($dataFiles[2]); showData(q{Read third file in list}); getLine(q{spw592341.dataD}); showData(q{Read spw592341.dataD}); foreach my $file (@dataFiles) { closeFile($file); } showData(q{After closing all files}); sub closeFile { my $file = shift; $rhFiles->{$file}->{handle}->close() or die qq{close: $file: $!\n}; delete $rhFiles->{$file}->{handle}; } sub getLine { my $file = shift; $rhFiles->{$file}->{buffer} = $rhFiles->{$file}->{handle}->getline(); chomp $rhFiles->{$file}->{buffer}; } sub openFile { my $file = shift; my $fh = IO::File->new($file, O_RDONLY) or die qq{open: $file: $!\n}; $rhFiles->{$file}->{handle} = $fh; } sub showData { my $msg = shift; print qq{\n$msg\n}; my $dd = Data::Dumper->new( [$rhFiles], [qw{rhFiles}])->Indent(1); print $dd->Dumpxs(); }

The output is just Data::Dumper output of the hash ref. used to hold the data.

Files to read spw592341.dataA spw592341.dataB spw592341.dataC spw592341.dataD After reading first line $rhFiles = { 'spw592341.dataB' => { 'handle' => bless( \*Symbol::GEN1, 'IO::File' ), 'buffer' => 'spw592341:dataB:line1' }, 'spw592341.dataD' => { 'handle' => bless( \*Symbol::GEN3, 'IO::File' ), 'buffer' => 'spw592341:dataD:line1' }, 'spw592341.dataA' => { 'handle' => bless( \*Symbol::GEN0, 'IO::File' ), 'buffer' => 'spw592341:dataA:line1' }, 'spw592341.dataC' => { 'handle' => bless( \*Symbol::GEN2, 'IO::File' ), 'buffer' => 'spw592341:dataC:line1' } }; All files read again $rhFiles = { 'spw592341.dataB' => { 'handle' => bless( \*Symbol::GEN1, 'IO::File' ), 'buffer' => 'spw592341:dataB:line2' }, 'spw592341.dataD' => { 'handle' => bless( \*Symbol::GEN3, 'IO::File' ), 'buffer' => 'spw592341:dataD:line2' }, 'spw592341.dataA' => { 'handle' => bless( \*Symbol::GEN0, 'IO::File' ), 'buffer' => 'spw592341:dataA:line2' }, 'spw592341.dataC' => { 'handle' => bless( \*Symbol::GEN2, 'IO::File' ), 'buffer' => 'spw592341:dataC:line2' } }; Read third file in list $rhFiles = { 'spw592341.dataB' => { 'handle' => bless( \*Symbol::GEN1, 'IO::File' ), 'buffer' => 'spw592341:dataB:line2' }, 'spw592341.dataD' => { 'handle' => bless( \*Symbol::GEN3, 'IO::File' ), 'buffer' => 'spw592341:dataD:line2' }, 'spw592341.dataA' => { 'handle' => bless( \*Symbol::GEN0, 'IO::File' ), 'buffer' => 'spw592341:dataA:line2' }, 'spw592341.dataC' => { 'handle' => bless( \*Symbol::GEN2, 'IO::File' ), 'buffer' => 'spw592341:dataC:line3' } }; Read spw592341.dataD $rhFiles = { 'spw592341.dataB' => { 'handle' => bless( \*Symbol::GEN1, 'IO::File' ), 'buffer' => 'spw592341:dataB:line2' }, 'spw592341.dataD' => { 'handle' => bless( \*Symbol::GEN3, 'IO::File' ), 'buffer' => 'spw592341:dataD:line3' }, 'spw592341.dataA' => { 'handle' => bless( \*Symbol::GEN0, 'IO::File' ), 'buffer' => 'spw592341:dataA:line2' }, 'spw592341.dataC' => { 'handle' => bless( \*Symbol::GEN2, 'IO::File' ), 'buffer' => 'spw592341:dataC:line3' } }; After closing all files $rhFiles = { 'spw592341.dataB' => { 'buffer' => 'spw592341:dataB:line2' }, 'spw592341.dataD' => { 'buffer' => 'spw592341:dataD:line3' }, 'spw592341.dataA' => { 'buffer' => 'spw592341:dataA:line2' }, 'spw592341.dataC' => { 'buffer' => 'spw592341:dataC:line3' } };

I hope this is of use.

Cheers,

JohnGG


In reply to Re: Using a filehandle tucked into an array by johngg
in thread Using a filehandle tucked into an array by redbeard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.