Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Introduce a counter into a loop within scala

Tags:

java

scala

I'm writing a small program which will convert a very large file into multiple smaller files, each file will contain 100 lines.

I'm iterating over a lines iteration :

  while (lines.hasNext) {
      val line = lines.next()
  }

I want to introduce a counter and when it reaches a certain value, reset the counter and proceed. In java I would do something like :

int counter = 0;
      while (lines.hasNext) {
          val line = lines.next()
if(counter == 100){
 counter = 0;
}
++counter
      }

Is there something similar in scala or an alternative method ?

like image 449
blue-sky Avatar asked Dec 12 '22 23:12

blue-sky


1 Answers

traditionally in scala you use .zipWithIndex

scala> List("foo","bar")
res0: List[java.lang.String] = List(foo, bar)

scala> for((x,i) <- res0.zipWithIndex) println(i + " : " +x)
0 : foo
1 : bar

(this will work with your lines too, as far as they are in Iterator, e.g. has hasNext and next() methods, or some other scala collection)

But if you need a complicated logic, like resetting counter, you may write it the same way as in java:

var counter = 0
while (lines.hasNext) {
  val line = lines.next()
  if(counter % 100 == 0) {
    // now write to another file
  }
}

Maybe you can tell us why you want to reset counter, so we may say how to do it better?

EDIT according to your update, that is better to do using grouped method, as @pr1001 proposed:

lines.grouped(100).foreach(l => l.foreach(/* write line to file*/))
like image 64
om-nom-nom Avatar answered Dec 26 '22 08:12

om-nom-nom