Let's say I have the following array of string:
val lines: List[String] = List("GOOL,1182", "AMZN,1920", "MSFT,124", "APPL,192.2")
In practice this type of array is typically obtained by reading a csv file.
Conceptually, I would like to
- For each line, split it by ","
- After splitting all the lines, assign the first column to a List, and the second column to another List.
The approach I came up with is the following:
var col1List = List[String]()
var col2List = List[String]()
lines.foreach{ x =>
val cols = x split ","
col1List = col1List ::: List(cols(0))
col2List = col2List ::: List(cols(1))
}
Afterward, I got the following Lists:
List[String] = List(GOOL, AMZN, MSFT, APPL)
List[String] = List(1182, 1920, 124, 192.2)
Is there a better way to do this in Scala?