Reputation: 8018
I have a scala list as
scala> c
res18: List[String] = List(123 asd, 234 zxc)
and i want something like this
List(List(123, asd),List(234, zxc))
how can i achieve that ? I tried
scala> var t = for (x <- c) x.split("\t")
t: Unit = ()
how can i split each element pf my list by space and then assign the result as a list of lists?
Upvotes: 0
Views: 560
Reputation: 14803
An alternative is to use map
, what usually is more readable if you have only one step:
c.map(_.split(" ").toList)
Upvotes: 7
Reputation: 26056
You need to use yield
keyword to return a list from for
loop:
var result = for (x <- c) yield x.split(" ")
But this will result in List[Array[String]], if you want to have inner
List`, use:
var result = for (x <- c) yield x.split(" ").toList
Upvotes: 1