Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Using more than one filehandle

by Anonymous Monk
on Mar 24, 2004 at 14:20 UTC ( #339406=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks: I am working on a script and at some point I have to open two text files at the same time and process the data from both at the same time as well, how do I do this trick, I am getting stuck trying to open the two files at once. Thanks

20040326 Edit by castaway: Changed title from 'Text Files'

Replies are listed 'Best First'.
Re: Using more than one filehandle
by jdporter (Canon) on Mar 24, 2004 at 14:38 UTC

    There is nothing to it. Two files, two filehandles. For example:

    my $filename1 = "f1.txt"; my $filename2 = "f2.txt"; open F1, "< $filename1" or die "read $filename1"; open F2, "< $filename2" or die "read $filename2"; # assuming the files are exactly parallel, line for line: while (<F1>) { my $line1 = $_; my $line2 = <F2>; # etc. } close F1; close F2;

    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Using more than one filehandle
by Aragorn (Curate) on Mar 24, 2004 at 14:45 UTC
    An untested example of a possible solution:
    open(FILE_A, "< file_a.txt") or die "Cannot open file_a: $!\n"; open(FILE_B, "< file_b.txt") or die "Cannot open file_b: $!\n"; while (1) { my $line_a = <FILE_A>; my $line_b = <FILE_B>; last if not defined $line_a or not defined $line_b; # Processing of $line_a and $line_b... } close(FILE_A); close(FILE_B);


Re: Using more than one filehandle
by Trimbach (Curate) on Mar 24, 2004 at 14:40 UTC
    open (FILE1, "file1.txt") or die "Can't read file1: $!"; open (FILE2, "file2.txt") or die "Can't read file2: $!"; #.... do whatever you need to each file, accessing them by the # appropriate filehandle.

    Gary Blackburn
    Trained Killer

Re: Using more than one filehandle
by matija (Priest) on Mar 24, 2004 at 14:41 UTC
    When you say "at once" do you mean, "at the exact same moment in time", or do you mean "I need to files opened at the same time"?

    If the former, no single CPU computer can do it, but some multiprocessor machines may be able to, provided their operating system supports it.

    If the later (more probable), then there is no problem:

    open(FILE1,"</some/file/somewhere") || die "Could not open FILE1: $!\n +"; open(FILE2,"</some/other/file") || die "Could not open FILE2: $!\n";
    At this point you have two files open and you can read from the first with <FILE1> and from the second with <FILE2>.

    Or, if you open the files for writing, you can write to them like this:

    print FILE1 "something to be written to FILE1\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://339406]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2020-12-04 16:58 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (60 votes). Check out past polls.