Prateek Agrawal
Prateek Agrawal

Reputation: 163

Searching Scala array to find a value and mapping it to (Array without the Value,Value)

I have a scala Array of the form :

temp: Array[(Array[String], Long)] 
# Eg.  Array((Array(attr1,1.0,attr2),15),(Array(1.0,attr5),15),(Array(attr3,attr4,0.0),15),(Array(attr3,attr4),5))

I need to take this Array and map it to something like

temp: Array[(Array[String],String, Long)] 
# Eg.  Array((Array(attr1,attr2),1.0,15),(Array(attr5),1.0,15),(Array(attr3,attr4),0.0,15),(Array(attr3,attr4),NULL,15))

I am searching for the string 1.0 and 0.0 and making a new array with 1.0 and 0.0 deleted from the original. Incase 1.0 and 0.0 not present then use NULL as the value. Is there an easy way to do this?

Upvotes: 0

Views: 141

Answers (1)

jwvh
jwvh

Reputation: 51271

Change the elements of a collection from one type to another type? Sounds like a map.

temp.map{ case (ss,l) =>
  ( ss.filterNot(_ matches "[01]\\.0")
  , ss.find(_ matches "[01]\\.0").getOrElse("NULL")
  , l )
}  // res0: Array[(Array[String], String, Long)]

Upvotes: 1

Related Questions