You actually have two errors in your code.
First as pointed out already you are using foreach (which returns Unit) instead of map (which returns an Object).
Second, you are using zip in your foreach function, which combines two collections pair wise, like this:
val arr1 = Seq("Apple", "Peach", "Banana")
val arr2 = Seq("Red", "Red", "Yellow")
val arr3 = arr1 zip arr2 // = Seq(("Apple", "Red"), ("Peach", "Red"), ("Banana", "Yellow"))
Your code should look like this:
val arr = Array("Id:1; Apple; Red; 2; Out",
"Id:2; Banana; Yellow; 5",
"Id:3; Peach; Red; 3",
"Id:4; Grape; Green; 5; Out")
arr.map(r => (r.split(";")(1), r.split(";")(3)))
Or to be a tiny bit more efficient, by splitting only once:
arr.map { r =>
val t = r.split(";")
(t(1), t(2))
}
And you probably don't want the white spaces, so:
arr.map { r =>
val t = r.split(";")
(t(1).trim(), t(2).trim())
}
Just adding this because I think regex is also a good option for string processing and a neat feature together with scala's pattern match:
val regEx = "[^;]+; ([^;]+); [^;]+; ([^;]+).*".r
arr collect {
case regEx(fruit, number) => (fruit, number)
}
Also leads to the desired output, but might be a bit overkill for this simple use case.
foreachreturnsUnit. Usemapinstead. You might also want to parse the numbers into integers (right now you will get strings).