03-11-2023, 02:23 PM
(02-28-2023, 06:24 PM)madscijr Wrote:(02-28-2023, 03:33 PM)Kernelpanic Wrote: There is a small error in the first code on the page. Thats how it works:
Code: (Select All)'Assosziatives Array: https://jeff.win/qbhash/
'28. Feb. 2023
Type element
tag As String * 10
value As String * 10
End Type
Dim Shared aa(10) As element
Dim Shared aalast ' Last occupied AA() element
setvalue "foo", "bar"
Print getvalue$("foo") ' prints bar
End
Function getvalue$ (tag As String)
tag = LTrim$(RTrim$(tag))
tag = tag + String$(10 - Len(tag), " ")
For i = 0 To aalast
If (tag = aa(i).tag) Then
getvalue$ = aa(i).value
Exit Function
End If
Next
End Function
Sub setvalue (tag As String, value As String)
aa(aalast).tag = tag
aa(aalast).value = value
aalast = aalast + 1
End Sub
One advantage of associative arrays didn't in moment become clear to me. Similar to a two-dimensional random access array. . . That's how I understood it.
Yes that can be quite useful in a lot of cases.
I haven't had a chance to test that code sample - is it broken?
Were you able to fix it?
I can give it a look later, and provide better examples that I did run and were working (at least they were a year or two ago, LoL!)
Althought the demo is interesting, it is so poor in aspects that is very far away from a dictionary while the hash setting is quiet good.
As you can see playing with the function/subroutines of hash array, the dictionary has been projected to have no duplicating of hash value, and to have one value for each hash value, this is very far from a dictionary where you can have a list of values associated with the hash value. (look at Bplus implementation of dictionary that is on the past forum and on Rosetta Code under the voice QB64 dictionary or associative arrays).
So to get a real dictionary from this first skeleton demo the function SetValue must have the managment of adding new value in tail (or ordered by value/character ) , so the same for removing (cancel) from the set of values associated to the hash value.
Indeed now this demo add the same hash key in a following cell of array, but this last is really unreached because the GetValue function returns only the first match with the hash value researched.
But with some modifications it is possible to get back a real dictionary with functions of storing, changing and cancelling the values associated to the hash value.
... but I think that is better the dictionary made by Bplus.