Dinusha Thilakarathna
Dinusha Thilakarathna

Reputation: 422

How to create list by deriving another list in scala

I have list as follow

List[String] = List(Gateway = MSC050, Sequence = 176484,Gateway = MSC050, Sequence = 176485)

I want to create another by referring above as follow

List[String] = List(50176484,50176485)

Here I have merger last two digit of Gateway and Sequence together into another list. Can any one help on this?

Upvotes: 0

Views: 303

Answers (2)

Teimuraz
Teimuraz

Reputation: 9335

val original: List[String] = List("MSC050", "176484", "MSC051", "176485")

val modified = original.grouped(2).map(el => el(0).takeRight(2) + el(1)).toList

Upvotes: 0

Tom
Tom

Reputation: 2254

Assuming your list is missing the quotes:

  val input: List[String] = List("Gateway = MSC050", "Sequence = 176484", "Gateway = MSC050", "Sequence = 176485")

  input.grouped(2).map {
    l => l(0).takeRight(2) + l(1).split(' ').last
  }.toList

Output:

List(50176484, 50176485)

grouped(2) will allow you to iterate over every Gateway/Sequence pair. Then you need to get the last two digits from the Gateway, and the number from the Sequence. I've done this with takeRight for the Gateway, and splitting on spaces for the sequence (assuming that your Sequence strings will always have a space after the equals).

Upvotes: 1

Related Questions