Compare two files line by line in scala -


i have 2 files, file1:

1 2 0 2 1 2 3 2 ... 

and file2:

1 2 2 2 1 1 2 1 ... 

how can compare these 2 files line line? want count lines have same values. example in 2 above files:

1=1 2=2 0!=2 2=2 ... 

i've tried:

 def main(args: array[string]): unit = {     val lines = fromfile("data/file1.txt").getlines     val lines2 = fromfile("data/file2.txt").getlines     var l = 0     var cnt = 0     (line <- lines) {       (line2<-lines2){          if (line == line2){           cnt += 1         }       }        println(cnt)      }   } 

but doesn't show favorite output.

you nesting iterations, i.e. compare line 1 of first file against all lines of second file, line 2 of first file against lines of second file… not because getlines gives iterator exhausted after first loop.

the easiest be

(lines1 zip lines2).count { case (a, b) => == b } 

Comments