Suppose you have already analyzed your data like this:
val myDoc = List( List("ID", "NAME", "DATE", "DESC"), List("1", "a", "1990", "x"), List("2", "b", "1991", "y") )
Now we can use splitAt
and unzip
to split the list. Please note that I accept a lot about the real data code, which we would like to check that the list is not empty and that the header actually contains the "DATE"
column.
def split(doc: Seq[Seq[String]]) = { val i = doc.head.indexOf("DATE") doc.map(_.splitAt(i)).unzip }
We can apply it to our test data:
scala> val (b, a) = split(myDoc) b: List[Seq[String]] = List(List(ID, NAME), List(1, a), List(2, b)) a: List[Seq[String]] = List(List(DATE, DESC), List(1990, x), List(1991, y))
It looks reasonable to me.
source share