Scala split string to tuple
list, scala, split, string, tuples
Solution
You could use pattern matching to extract what you need from the array:
case class Point(pts: Seq[Double])
val lines = List("1 1 4.34 2.34")
val coords = lines.collect(_.split("\\s+") match {
case Array(s1, s2, points @ _*) => (s1, s2, Point(points.map(_.toDouble)))
})
Problem
I would like to split a string on whitespace that has 4 elements: ``` 1 1 4.57 0.83 ``` and I am trying to convert into List[(String,String,Point)] such that first two splits are first two elements in the list and the last two is Point. I am doing the following but it doesn't seem to work: ``` Source.fromFile(filename).getLines.map(string => { val split = string.split(" ") (split(0), split(1), split(2)) }).map{t => List(t._1, t._2, t._3)}.toIterator ```