Reputation: 44051
val files = new File("data").list()
val filtered = files.filter(name => name.contains("txn"))
val masterList = new ListBuffer[String]
for (file <- filtered) {
val lines = Source.fromFile(new File("data\\" + file), "UTF-16").getLines
val cleaned = lines.filter(!masterList.contains(_))
println("*" + cleaned.length)
cleaned.foreach(println(_))
println("**" + cleaned.length)
cleaned.foreach(masterList.append(_))
}
The output from the code is as follows
*175
**0
I don't understand why my list disappears
Upvotes: 3
Views: 2769
Reputation: 41646
Source.fromFile(new File("data\\" + file), "UTF-16").getLines
returns an Iterator[String]
. Filtering still returns an Iterator
. Calling length
on an iterator will cause it to be read fully until hasNext
returns false. The second time around, it's empty. Similar to:
scala> val cleaned = List(1,2).iterator
cleaned: Iterator[Int] = non-empty iterator
scala> cleaned.length
res10: Int = 2
scala> cleaned.length
res11: Int = 0
Upvotes: 6
Reputation: 62835
cleaned
is an iterator.
scala> val cleaned = lines.filter(_!="")
cleaned: Iterator[String] = non-empty iterator
Right after assigning it is non-empty. Iterators in scala are single-used - once you traversed it (e.g. by applying length
method) it becomes empty:
scala> cleaned.length
res0: Int = 13
scala> cleaned.length
res1: Int = 0
You can fix that behavior by converting to List, or to Seq (lazy):
scala> val lines=Source.fromFile("bla.txt").getLines
lines: Iterator[String] = non-empty iterator
scala> val cleaned = lines.filter(_!="").toSeq
cleaned: Seq[String] = Stream(first, ?)
scala> cleaned.length
res4: Int = 13
scala> cleaned.length
res5: Int = 13
Upvotes: 19