, 1 min read

Reading Two Files Simultaneously With Perl

Original post is here eklausmeier.goip.de/blog/2024/11-10-reading-two-files-simultaneously-with-perl.


Task at hand: Read two or more files at the same time in Perl.

Solution:

#!/bin/perl -W
# Reading from multiple files simultaenously

use strict;

my ($f1,$f2);

open(F1,"< $ARGV[0]") || die("Cannot open $ARGV[0]");
open(F2,"< $ARGV[1]") || die("Cannot open $ARGV[1]");


while (defined($f1 = <F1>) && defined($f2 = <F2>)) {
    printf("f1=|%s|, f2=|%s|\n",$f1,$f2);
}