Dictionary search

I have this dictionary:

RowDic = new Dictionary
RowDic.Value(0) = “0,1,2,9,10,11,18,19,20”
RowDic.Value(1) = “3,4,5,12,13,14,21,22”
RowDic.Value(2) = “6,7,8,15,16,17,24,25,26”
RowDic.Value(3) = “27,28,29,36,37,38,45,46,47”
RowDic.Value(4) = “30,31,32,39,40,41,48,49,50”
RowDic.Value(5) = “33,34,35,42,43,44,51,52,53”
RowDic.Value(6) = “54,55,56,63,64,65,72,73,74”
RowDic.Value(7) = “57,58,59,66,67,68,75,76,77”
RowDic.Value(8) = “60,61,62,69,70,71,78,79,80”

I can obtain the values by passing the key. But can I search in the values and return the key?
So I have value say 31. I want to have the corresponding key (4). Is this possible in a way?

[Edit] I found that reverse lookup seems not possible…

Thanks

Here’s a quick method I wrote:

Public Function findRow(inDict as Dictionary, value as Integer) as Integer
  if inDict = nil then Return -1
  
  var intMax as Integer = inDict.KeyCount - 1
  
  var currentKey as Integer
  var currentValue as String
  
  for intCycle as Integer = 0 to intMax
    currentKey = inDict.Key(intCycle)
    currentValue = inDict.Value(currentKey)
    
    if currentValue.Split( "," ).IndexOf( value.ToString ) > -1 then
      Return currentKey
    end if
  next
  
  Return -1
End Function

To be used as:

var RowDic as new Dictionary
RowDic.Value(0) = "0,1,2,9,10,11,18,19,20"
RowDic.Value(1) = "3,4,5,12,13,14,21,22"
RowDic.Value(2) = "6,7,8,15,16,17,24,25,26"
RowDic.Value(3) = "27,28,29,36,37,38,45,46,47"
RowDic.Value(4) = "30,31,32,39,40,41,48,49,50"
RowDic.Value(5) = "33,34,35,42,43,44,51,52,53"
RowDic.Value(6) = "54,55,56,63,64,65,72,73,74"
RowDic.Value(7) = "57,58,59,66,67,68,75,76,77"
RowDic.Value(8) = "60,61,62,69,70,71,78,79,80"

var valueKey as Integer = findRow( RowDic, 31 )
if valueKey > -1 then
  MessageBox( "Found on row " + valueKey.ToString )
end if
1 Like

That works very well!

Thank you Anthony.

1 Like

Happy to help! Please mark as the solution so others can easily see it should they go looking in the future.

1 Like

You can also maintain a second Dictionary for the reverse lookup.