in reply to compare two files line by line and perform a diff on each coressponding line

If you only want to compare each line of tst1 to the corresponding line in tst2 you don't need two loops. Use something like this:

while (@tst1) { $i= shift @tst1; $j= shift @tst2; chomp($i); chomp($j); `diff $i $j > /dev/null`; `echo $?`; }

You thought you needed two loops because you wanted to step through two arrays, but you need only one loop that makes a simultaneous step through both arrays.

  • Comment on Re: compare two files line by line and perform a diff on each coressponding line
  • Download Code

Replies are listed 'Best First'.
Re^2: compare two files line by line and perform a diff on each coressponding line
by dilip_val (Acolyte) on Jun 01, 2010 at 15:33 UTC
    Thanks jethro and all , this seemed to work
    #! /usr/bin/perl use File::Compare; open (IF,"<tst1"); open (IF2,"<tst2"); my(@tst1) = <IF>; my(@tst2) = <IF2>; my ($i); my ($j); while (@tst1) { $i=shift @tst1; $j= shift @tst2; chomp($i); chomp($j); #print "$i\n"; #print "$j\n"; if (compare("$i","$j") == 0) { print "$i $j are equal\n"; } else { print "$i $j are not equal\n"; } } close(IF); close(IF2);