ErosOlmi
22-04-2007, 10:49
Here a not so clever example but can give you an idea of what is also possible to do with Dictionaries.
You can store and retrieve full data structure. Because data stored into a Dictionary is not limited to strings but the full ASCII set is supported, you can store any kind of data into a dictionary: a string, a structure, a file of any type, numbers in their binary format, ...
USES "Dictionary"
'---Define a structure
type MyType
Name as asciiz * 32
NumberLong as long
NumberExtended as ext
FixedString as string * 256
FixedAsciiZ as asciiz * 256
ALittleArray(10) as long
ABiggerArray(200) as long
end type
msgbox 0, "Size of structure: " & sizeof(MyType)
'---Define a LONG that will hold a reference to a new dictionary
DIM MyDict AS LONG
'---Create a new dictionary with 1000 unique single buckets
MyDict = Dictionary_Create(1000, %true)
dim dummyReset as MyType '---Used to reset/clean variable type
dim dummyVarType as MyType '---Used to store temp data
'---Fill first structure and add to the dictionary
dummyVarType.Name = "Robot1"
dummyVarType.NumberLong = 12345678
Dictionary_Add(MyDict, dummyVarType.Name , dummyVarType)
msgbox 0, "Added: '" & dummyVarType.Name & "' to dictionary"
'---Fill first structure and add to the dictionary
dummyVarType = dummyReset '---Before reusing dummyVarTpe, better to reset to initial value
dummyVarType.Name = "Robot2"
dummyVarType.NumberLong = 67345
Dictionary_Add(MyDict, dummyVarType.Name , dummyVarType)
msgbox 0, "Added: '" & dummyVarType.Name & "' to dictionary"
'---Now search and get back data
dummyVarType = Dictionary_Find(MyDict, "Robot1")
msgbox 0, "Current structure name: " & dummyVarType.Name
'---Remove the whole dictionary and all its keys and data from memory
Dictionary_Free(MyDict)
You can store and retrieve full data structure. Because data stored into a Dictionary is not limited to strings but the full ASCII set is supported, you can store any kind of data into a dictionary: a string, a structure, a file of any type, numbers in their binary format, ...
USES "Dictionary"
'---Define a structure
type MyType
Name as asciiz * 32
NumberLong as long
NumberExtended as ext
FixedString as string * 256
FixedAsciiZ as asciiz * 256
ALittleArray(10) as long
ABiggerArray(200) as long
end type
msgbox 0, "Size of structure: " & sizeof(MyType)
'---Define a LONG that will hold a reference to a new dictionary
DIM MyDict AS LONG
'---Create a new dictionary with 1000 unique single buckets
MyDict = Dictionary_Create(1000, %true)
dim dummyReset as MyType '---Used to reset/clean variable type
dim dummyVarType as MyType '---Used to store temp data
'---Fill first structure and add to the dictionary
dummyVarType.Name = "Robot1"
dummyVarType.NumberLong = 12345678
Dictionary_Add(MyDict, dummyVarType.Name , dummyVarType)
msgbox 0, "Added: '" & dummyVarType.Name & "' to dictionary"
'---Fill first structure and add to the dictionary
dummyVarType = dummyReset '---Before reusing dummyVarTpe, better to reset to initial value
dummyVarType.Name = "Robot2"
dummyVarType.NumberLong = 67345
Dictionary_Add(MyDict, dummyVarType.Name , dummyVarType)
msgbox 0, "Added: '" & dummyVarType.Name & "' to dictionary"
'---Now search and get back data
dummyVarType = Dictionary_Find(MyDict, "Robot1")
msgbox 0, "Current structure name: " & dummyVarType.Name
'---Remove the whole dictionary and all its keys and data from memory
Dictionary_Free(MyDict)