0

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?

2
  • What's the proper procedure if there is more than one "0.0" or "1.0"? Commented Jun 15, 2017 at 21:06
  • there won't be any duplicates the array has unique values for sure. Commented Jun 15, 2017 at 21:23

1 Answer 1

1

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)]
Sign up to request clarification or add additional context in comments.

4 Comments

Do I need to import anything I am getting this error: <console>:3: error: illegal start of simple expression
No import needed. This code only assumes that a val named temp exists and it is of type Array[(Array[String], Long)].
I was using () instead of {}. Brilliant. How did you know about the filterNot function. Is there some guide to reach to such cool functions?
I keep a tab to the Scala Standard Library open and available at all times. Sometimes I just wander through the pages just to see what new things I can find.

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.