in reply to Re: read from two files with nested while loops
in thread read from two files with nested while loops
Better still, though, would be to slurp B into an array instead of re-reading the file for every line in A:open my $fhA, '<', '1.dat' or die $!; open my $fhB, '<', '2.dat' or die $!; while (my $lineA=<$fhA>){ seek $fhB, 0, 0; while(my $lineB=<$fhB>){ print $lineA,' ', $lineB; } }
(And I also switched both examples to use lexical filehandles and three-arg open instead of doing it The Old-Style Way because they're better/safer/etc.)open $fhA, '<', '1.dat' or die $!; open $fhB, '<', '2.dat' or die $!; @B_lines = <$fhB>; while (my $lineA=<$fhA>){ for my $lineB (@B_lines) { print $lineA,' ', $lineB; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: read from two files with nested while loops
by diamantis (Beadle) on Feb 09, 2011 at 15:49 UTC |