Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: reading lines from 2 different files

by toolic (Bishop)
on Oct 08, 2007 at 15:44 UTC ( #643488=note: print w/replies, xml ) Need Help??

in reply to reading lines from 2 different files

If you want to compare all lines of the 2 files, just like the unix "diff" command (or "tkdiff"):
use warnings; use strict; open my $fh1, '<', 'file1.txt' or die "Can not open file1.txt $!\n"; open my $fh2, '<', 'file2.txt' or die "Can not open file2.txt $!\n"; while (<$fh1>) { chomp; my $line_f2 = <$fh2>; chomp $line_f2; if ($_ ne $line_f2) { print "Mismatch at line $.\n"; print "file1: $_, file2: $line_f2\n"; } } close $fh2; close $fh1;

Replies are listed 'Best First'.
Re^2: reading lines from 2 different files
by ikegami (Patriarch) on Oct 08, 2007 at 18:58 UTC
    Your code silently ignores extra lines in file2.
      Yes, I realize that, but thanks for explicitly pointing it out. The OP stated that the 2 files have the "same number of lines". So, I presumed that the OP would be checking that ahead of time.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://643488]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2023-03-27 22:15 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (66 votes). Check out past polls.