2

enter image description herehere is my sample input:

val list=List("a;bc:de;f","uvw:xy;z","123:456")

I am applying following operation

val upper=list.map(x=>x.split(":")).map(x=>x.split(";"))

but it is throwing error- error: value split is not a member of Array[String]

can anyone help how to use both split so that i can get answer!

Thank you in advance.

blackbishop
  • 30,945
  • 11
  • 55
  • 76
Aditya Ranjan
  • 135
  • 1
  • 2
  • 11
  • `list.map(x=>x.split(":"))` will give you a list of Array. Then you are trying to run `.map` an for each item split on an array. I think you want `val upper=list.map(x=>x.split(":").map(x=>x.split(";")))` – The fourth bird Feb 19 '20 at 18:16
  • i have to iterate val upper=list.map(x=>x.split(":")).map(x=>x.split(";"))<- from last bracket then how can i do it? – Aditya Ranjan Feb 19 '20 at 18:24

3 Answers3

2

Using list.map(x=>x.split(":")) will give you a list of Array.

upper: List[Array[String]] = List(Array(a;bc, de;f), Array(uvw, xy;z), Array(123, 456))

Mapping afterwards, you can see that the item will be an array where you are trying to run split on.

You might useflatMap instead which will first give you List(a;bc, de;f, uvw, xy;z, 123, 456) and then you can use map on those items splitting on ;

val upper = list.flatMap(_.split(":")).map(_.split(";"))

Output

upper: List[Array[String]] = List(Array(a, bc), Array(de, f), Array(uvw), Array(xy, z), Array(123), Array(456))
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
2

You can use split with multiple delimiters in one map iteration :

val upper = list.map(x => x.split("[:;]"))

//upper: List[Array[String]] = List(Array(a, bc, de, f), Array(uvw, xy, z), Array(123, 456))
blackbishop
  • 30,945
  • 11
  • 55
  • 76
0

Here is the code i have tried and it worked:

val upper=list.map(x=>x.split(":")).map(x=>x.map(x=>x.split(";")))

which gives the output:

upper: List[Array[Array[String]]] = List(Array(Array(a, bc), Array(de, f)), Array(Array(uvw), Array(xy, z)), Array(Array(123), Array(456)))
Aditya Ranjan
  • 135
  • 1
  • 2
  • 11