1

I have a scala list which looks like:

val list = List("2.56.7", "1.34.67")

I want a O/P like this:

List(List("2", "56", "7"), List("1", "34", "67")

I tried the below snippet but didnt work:

list.map(_.split("\\."))

I want a O/P like this:

List(List("2", "5", "6"), List("1", "34", "67")
3
  • I don't follow your logic. Can you better explain the transformation from the initial list into the output you expect? Commented Dec 25, 2018 at 5:03
  • i want to unpack all elements based on . so it has to look like [2, 56, 7], [ 1, 34, 67] . to be precise i want to split based on . for all elem in list] Commented Dec 25, 2018 at 5:11
  • I was able to get something working below. I'm not a Scala person, by the way. Commented Dec 25, 2018 at 5:20

2 Answers 2

4

Your code is almost right, but split returns Array not List so you need to convert the result to List.

list.map(_.split("\\.").toList)
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a working script. We can try using map on the outer list, with a lambda which maps each string to a another map of elements.

val list = List("2.56.7", "1.34.67")
val result = list.map(x => x.split("\\.").map(_.trim).toList)

def printList(args: List[_]): Unit = {
  args.foreach(println)
}
printList(result)

List(2, 56, 7)
List(1, 34, 67)

3 Comments

wow fantastic this is what i want. Thanks a lot +1 @Tim Biegeleisen
sure. Can i apply a sort based on each element of sub-list ?
What is the new output you want? Also, if it be very different than your original question, you might want to open a new question.

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.