0

I have an input string

val x = "snapshot_year_month=201610,snapshot_day=05,source='zzz'"
val y = x.split(",")

This produces a 3 part array y. Now, I want to take each item in y and split it again by = and have the final output in Array[Array[String]. How would I do this? I tried the following but it did not work.

var finalSplit = y.foreach(z => z.split("="))
2
  • Variable size. @YuvalItzchakov I know I could create a mutable array, append to it over each iteration, but this seems like a poor approach. Commented Oct 17, 2018 at 14:46
  • .foreach returns a Unit. It doesn't return anything (at least, it doesn't return anything you can later manipulate). Use .map instead. Commented Oct 17, 2018 at 14:50

1 Answer 1

2

You need to use .map instead of .foreach. The latter returns Unit, which is meant to execute a side effect and not return any value:

val data = "snapshot_year_month=201610,snapshot_day=05,source='zzz'"
val splitData = x.split(",")

val finalRes: Array[Array[String]] = splitData.map(_.split("="))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.