Sunday, February 2, 2014

Split a List by an Empty Line

A function that splits a list by an empty line. The last list element is empty. The function is based on  this post from the StackOverflow.

def splitByEmptyLine[String]( l: Seq[String]): Seq[Seq[String]] = {
 val b = ListBuffer(ListBuffer[String]())
  l.foreach { e =>  
   if ( e.toString().isEmpty() ) {  
     if  ( !b.last.isEmpty )
     b += ListBuffer[String]()
   }
   else
     b.last += e
 }
 b.map(_.toSeq)
}

No comments:

Post a Comment