I'm a long user of arrays in VBA but I recently learned a bit about hashing and I was wondering if I could use that to build more efficient searches in my arrays. To keep it specific, what I did was to turn a two dimensional array into a dictionary of rows where the keys is a string (which off course is unique) found in a 'cell' and turned into a double via asc.
I guess the code below explains what I mean:
Private pHook As Object
Sub test()
Set pHook = CreateObject("Scripting.Dictionary")
key = StoAsc("SomeStringOneWantstoFind")
If Not pHook.Exists(key) Then pHook.Add key, "TEST"
d = pHook(key)
End Sub
Public Function StoAsc(stg As String) As Double:
Dim key As String
key = ""
For ii = 1 To Len(stg)
S = Asc(Mid(stg, ii, 1))
key = key & S
Next ii
StoAsc = CDbl(key)
End Function
It looks like it works and it did the job of avoiding a the loop when I just want to find something in the data.
But I can't get out of my mind the idea that there should be a easier and more logical path than building the hashing myself. Am I in a good path? Are there easier ways to 'hash an array' so don't have to loop around every time I need something?