2

I have first file with data as

A,B,C
B,E,F
C,N,P

And second file with data as below

A,B,C,YES
B,C,D,NO
C,D,E,TRUE
D,E,F,FALSE
E,F,G,NO

I need every record in the first file to iterate with all records in the second file. But it's happening only for the first record.

Below is the code:

import scala.io.Source.fromFile

object TestComparision  {

    def main(args: Array[String]): Unit = {
      val lines = fromFile("C:\\Users\\nreddy26\\Desktop\\Spark\\PRI.txt").getLines
      val lines2 = fromFile("C:\\Users\\nreddy26\\Desktop\\Spark\\LKP.txt").getLines
      var l = 0
      var cnt = 0
      for (line <- lines) {

        for (line2 <- lines2) {

          val cols = line.split(",").map(_.trim)
          println(s"${cols(0)}|${cols(1)}|${cols(2)}")

          val cols2 = line2.split(",").map(_.trim)
          println(s"${cols2(0)}|${cols2(1)}|${cols2(2)}|${cols2(3)}")


        }
      }
    }
  }
4
  • Were able to get the output like this below: A,B,C A,B,C,YES A,B,C B,C,D,NO A,B,C C,D,E,TRUE A,B,C D,E,F,FALSE A,B,C E,F,G,NO B,E,F A,B,C,YES B,E,F B,C,D,NO B,E,F C,D,E,TRUE B,E,F D,E,F,FALSE B,E,F E,F,G,NO C,N,P A,B,C,YES C,N,P B,C,D,NO C,N,P C,D,E,TRUE C,N,P D,E,F,FALSE C,N,P E,F,G,NO Commented May 13, 2021 at 13:16
  • every row in first set should iterate with second set of data but for me its happening only for first record. Commented May 13, 2021 at 13:22
  • 3
    Iterators are only consumed once, so you either need to re-open the file every time, or cache their contents in memory by turning them into a List or a Vector. Commented May 13, 2021 at 13:57
  • thanks will perform it Commented May 17, 2021 at 6:03

1 Answer 1

1

As rightly suggested by @Luis, get the lines in List form by using toList:

val lines = fromFile("C:\\Users\\nreddy26\\Desktop\\Spark\\PRI.txt").getLines.toList
val lines2 = fromFile("C:\\Users\\nreddy26\\Desktop\\Spark\\LKP.txt").getLines.toList
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.