Android Question Performing mathematical functions between 2 maps of values

JonnyCav

Member
Or am I barking up the wrong tree?
(All sqlite)
Here's the theory based on the fact I have one table (T1) that continually accrues data over time.
Now, at a certain time(button call) TWO sqlite result sets are created
I put the first set from T1 into a map, map1
I put the second result set into another map, map2

Now I wish to ForEach in map2 DIVIDE the VALUE of map2 by the value of map1

Possible?
 
Solution
You could use a third map which would look up the matching key in mp1.
When the keys are different and you want to avoid "hard coding" the pairings, you will need some kind of dictionary.

B4X:
    Dim mp1 As Map
    mp1.Initialize
    mp1.Put("c1", 6)
    mp1.Put("c2", 17)
    mp1.Put("c3", 25)
    mp1.Put("c4", 15)
    mp1.Put("c5", 2)
        
    Dim mp2 As Map
    mp2.Initialize
    mp2.Put("AAAAA", 43)
    mp2.Put("BBBBB", 100)
    mp2.Put("CCCCC", 12)
    mp2.Put("DDDDD", 6)

    Dim lookup As Map = CreateMap("AAAAA": "c1", "BBBBB": "c2", "CCCCC": "c3", "DDDDD": "something else")

    For Each k As String In mp2.Keys
        Dim matchingKey As String = lookup.GetDefault(k, "N/A")
        Log(mp2.GetDefault(k, 0/0) /...

Mahares

Expert
Licensed User
Longtime User
Possible?
Something like this will work for you:
B4X:
Dim mp1 As Map
    mp1.Initialize
    Dim v1 As Int = 6
    Dim v2 As Int = 17
    Dim v3 As Int = 25    
    mp1.Put("first", v1)
    mp1.Put("second", v2)
    mp1.Put("third", v3)
    
    Dim mp2 As Map
    mp2.Initialize
    Dim v1 As Int = 43
    Dim v2 As Int = 100
    Dim v3 As Int = 63
    mp2.Put("first", v1)
    mp2.Put("second", v2)
    mp2.Put("third", v3)
    
    For Each k As String In mp2.Keys
        Log(mp2.Get(k)/mp1.Get(k))
    Next
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
More information will be needed. Will both maps be of the same size (both contain the same number of elements)? Will both maps have the same keys? I've just been ninja'd by @Mahares. Note that his code assumes that both maps are of the same size and both maps contain the same keys. If those conditions are true, @Mahares is the code to go with.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
@OliverA

With a small change #2 will work in all cases without throwing an error.

B4X:
    Dim mp1 As Map
    mp1.Initialize
    Dim v1 As Int = 6
    Dim v2 As Int = 17
    Dim v3 As Int = 25
    mp1.Put("first", v1)
    mp1.Put("second", v2)
    mp1.Put("third", v3)
    mp1.Put("aaaaa", v3)
    mp1.Put("bbbbb", v3)
   
    Dim mp2 As Map
    mp2.Initialize
    Dim v1 As Int = 43
    Dim v2 As Int = 100
    Dim v3 As Int = 63
    mp2.Put("first", v1)
    mp2.Put("second", v2)
    mp2.Put("third", v3)
    mp2.Put("xxxxx", v3)

    For Each k As String In mp2.Keys
        Log(mp2.GetDefault(k, 0/0)/mp1.GetDefault(k, 0/0))
    Next

   
'7.166666666666667
'5.882352941176471
'2.52
'NaN
 
Upvote 0

JonnyCav

Member
Thank you all for the replies. I see how that works perfectly but unfortunately my main 'map' doesn't hold the same key as my other maps. I am working on that.

I have found this post that I think might help me.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
You could use a third map which would look up the matching key in mp1.
When the keys are different and you want to avoid "hard coding" the pairings, you will need some kind of dictionary.

B4X:
    Dim mp1 As Map
    mp1.Initialize
    mp1.Put("c1", 6)
    mp1.Put("c2", 17)
    mp1.Put("c3", 25)
    mp1.Put("c4", 15)
    mp1.Put("c5", 2)
        
    Dim mp2 As Map
    mp2.Initialize
    mp2.Put("AAAAA", 43)
    mp2.Put("BBBBB", 100)
    mp2.Put("CCCCC", 12)
    mp2.Put("DDDDD", 6)

    Dim lookup As Map = CreateMap("AAAAA": "c1", "BBBBB": "c2", "CCCCC": "c3", "DDDDD": "something else")

    For Each k As String In mp2.Keys
        Dim matchingKey As String = lookup.GetDefault(k, "N/A")
        Log(mp2.GetDefault(k, 0/0) / mp1.GetDefault(matchingKey, 0/0))
    Next
 
Upvote 0
Solution

JonnyCav

Member
William. Thanks. That certainly helped and I have applied it to another aspect of my code. But for this particular problem (because it would have taken 5 maps), I tweaked the database instead and then did the division in sqlite.
 
Upvote 0
Top