diens
diens

Reputation: 659

Scala: Using a value from an array to search in another array

I have two arrays:

GlobalArray:Array(Int,Array[String]) and SpecificArray:Array(Int,Int).

The first Int in both of them is a key and I would like to get the element corresponding to that key from the GlobalArray.

In pseudocode:

val v1
For each element of SpecificArray
    Get the corresponding element from GlobalArray to use its Array[String]
         If (sum <= 100)
             for each String of the Array
                 update v1 
                 // ... some calculation
                 sum += 1

println (v1)

I know using .map() I could go through each position of the SpecificArray, but so far I was able to do this:

SpecificArray.map{x => val in_global = GlobalArray.filter(e => (e._1 == x._1))
                       // I don't know how to follow
                  }

Upvotes: 0

Views: 876

Answers (3)

SergGr
SergGr

Reputation: 23788

How sum affects the logic and what exactly is v1 is not clear from your code, but it looks like you do search through GlobalArray many times. If this is so, it makes sense to convert this array into a more search-friendly data structure: Map. You can do it like this

val globalMap = GlobalArray.toMap

and then you may use to join the strings like this

println(SpecificArray.flatMap({case (k,v) => globalMap(k).map(s => (k,v,s))}).toList)

If all you need is strings you may use just

println(SpecificArray.flatMap({case (k,v) => globalMap(k)}).toList)

Note that this code assumes that for every key in the SpecificArray there will be a matching key in the GlobalArray. If this is not the case, you should use some other method to access the Map like getOrElse:

println(SpecificArray.flatMap({case (k,v) => globalMap.getOrElse(k, Array()).map(s => (k,v,s))}).toList)

Update

If sum is actually count and it works for whole "joined" data rather than for each key in the SpecificArray, you may use take instead of it. Code would go like this:

val joined = SpecificArray2.flatMap({case (k,v) => globalMap.getOrElse(k, Array()).map(s => (s,v))})
        .take(100) // use take instead of sum

And then you may use joined whatever way you want. And updated demo that builds v1 as joined string of form v1 += String_of_GlobalArray + " = " + 2nd_Int_of_SpecificArray is here. The idea is to use mkString instead of explicit variable update.

Upvotes: 1

Binzi Cao
Binzi Cao

Reputation: 1085

How about something like below, I would prefer to for comprehension code which has better readability.

var sum:Int = 0
var result:String = ""
for {
  (k1,v1) <- SpecificArray //v1 is the second int from specific array
  (k2,values) <- GlobalArray if k1 == k2  //values is the second array from global array
  value  <- values if sum < 100  //value is one value frome the values
   _ = {sum+=1; result += s"(${v1}=${value})"} //Update something here with the v1 and value
 } yield ()

println(result)

Upvotes: 1

shanmuga
shanmuga

Reputation: 4499

Note needs more optimization

Convert GlobalArray to Map for faster lookup.

val GlobalMap = GlobalArray.toMap

SpecificArray.flatMap(x => GlobalMap(x._1))
.foldLeft(0)((sum:Int, s:String) => {
    if(sum<=100) {
    // update v1
    // some calculation
    }
    sum+1
})

If not all keys of SpecificArray is present in GlobalMap then use GlobalMap.getOrElse(x._1, Array())

Upvotes: 1

Related Questions