Reputation:
I am still learning Scala and I am facing the following issue. Currently I have the following list in input
val listA=List("banana,africa,1,0",
"apple,europe,1,2",
"peas,africa,1,4")
The wanted output is :
val listB=list("banana,africa,1,0,1",
"apple,europe,1,2,3",
"peas,africa,1,4,5")
My aim is to add an element corresponding to the sum of the two last elements for each line in the list. I wrote the following basic function
def addSum(listin:List[String]):List[String]= {
listin.map(_.split(",")).map(d => d + "," + d(2)+d(3))
}
this is not working any suggestion aboit a better way to do it please
Thanks a lot
Upvotes: 0
Views: 61
Reputation: 41987
Simple solution is to do something like below
listA.map(str => str.split(",")).map(arr => (arr ++ Array(arr(2).toInt+arr(3).toInt)).mkString(","))
Upvotes: 1