iterating over a map question

Ricky D

Well-Known Member
Licensed User
Longtime User
I have a map named AcesMap and consists of int keys and string values like

1 Aspirin
2 Coversyl

I know I can iterate over the map like this

B4X:
For Each s As String In AcesMap.Values
    If s=Value Then
'get the key but how?
        Exit
    End If
Next

How do I retrieve the key from the map?

regards, Ricky
 

Bill Norris

Active Member
Licensed User
Longtime User
iterate with an index instead of for/each:

(I believe map elements are zero-based)

For i = 0 To yourmap.Size-1
If yourmap.GetValueAt(i)=yourvalue Then
keyvalue=yourmap.GetKeyAt(i)
next
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
You're right. Thanks. I thought there might have been a way using For Each.

regards, Ricky

P.S yes maps are zero based
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
If you want to use For Each then you will have to do something like this:
B4X:
Dim myMap As Map
            
myMap.Initialize
            
For I = 0 To 9
            
    myMap.Put("Key " & I, "Value " & I)
                        
Next            

For Each myVal As String In myMap.Values 'Here we get the values
            
    Msgbox("Value = " & myVal, "")
                        
Next   

For Each myKey As String In myMap.Keys 'Here we get the keys
            
    Msgbox("Found keys = " & myKey, "")
                        
Next
 
Last edited:
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
@NJDude - what I am wanting is to match one of the values in the For Each then retrieve the key associated with it then exit the loop.

I have it working using the old For i=0 To map.Size-1

Here is the complete sub

B4X:
Sub aceInsulin1_ItemClick (Value As String)
   For i=0 To AcesMap.Size-1 
      If AcesMap.GetValueAt(i)=Value Then
         Insulin1Id = AcesMap.GetKeyAt(i)
         changesmade(True)
         Exit
      End If
   Next
End Sub

regards, Ricky
 
Upvote 0

Azlan

Member
Licensed User
Longtime User
There is another approach that uses a Type instead of a Map and a List as the collection. Using a Type allows for more values.

B4X:
Sub Process_Globals

   Type obj(Key As String, Value As String, SomeOtherValue As Int)
   Dim MyListOfObjects As List

End Sub

Sub Activity_Create(FirstTime As Boolean)
                   
   MyListOfObjects.Initialize
                
   For I = 0 To 9
   
       Dim tmp As obj
      tmp.Key = "Key " & I
      tmp.Value = "Value " & I
      tmp.SomeOtherValue = I
      MyListOfObjects.Add(tmp)
   Next  
   
   Log( GetInfo("Key 2").Value )

End Sub

Sub GetInfo(key As String) As obj

   For Each o As obj In MyListOfObjects
      If o.key = key Then
           Return o            
      End If 
   Next

End Sub

Cheers
 
Last edited:
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
here is the working sub with Erel's tip:

B4X:
Sub aceInsulin1_ItemClick (Value As String)
   For Each key In AcesMap.Keys 
      If AcesMap.Get(key)=Value Then
         Insulin1Id = key
         changesmade(True)
         Exit
      End If
   Next
End Sub

works just how I want.

Thanks Erel.

regards, Ricky
 
Upvote 0
Top