<< Click to Display Table of Contents >> Navigation: ThinBASIC Core Language > Data Structures > cHash |
Description
cHash data structure is very similar to an Hash Table object with key/value pairs.
It is also similar to cOptions object but more flexible: elements can be defined dynamically wthout the need to be defined first.
It can be used to store program parameters or objects elements.
There are many different ways to add key names to a cHash object
Key names can be specified in free form or using strings or string expressions.
Imagine to have a CHASH object like
Dim oHash as new CHASH
here below some examples on how to specify key name
▪Using .Add method
oHash.Add(ElementName: "Value")
▪dynamically specifying an unknown element name
oHash.ElementName = "Value"
▪dotted notation
oHash.ElementName.anysubname = "Value"
▪backslash notation
oHash.Element\Name\Sub\Name = "Value"
▪starting with a double quote string it will be interpreted as a string expression.
IMPORTANT: in this case a semi colon is needed to assign a value
oHash."ElementName" : "Value"
▪starting with $ interpolation function will be interpreted as a string expression.
▪IMPORTANT: in this case a semi colon is needed to assign a value
oHash.$("Element{left$(""name_xyzw"", 4)}") : "Value___$"
Example
cHash example showing how to add and use new elements on the fly to a cHash object
uses "console"
dim oHash as new cHash
'---Elements name can be added on the fly to oHash object just specifying an unknown element name
oHash.x = rnd(1, 1000)
oHash.y = rnd(1, oHash.y)
oHash.z = rnd(1, oHash.z)
'---Show oHash structure
printl oHash.ToString
'---Elements name can have \ or . as part of the name allowing names with complex meaning
oHash.x.y.x.point = "This is a complex element key name composed with ."
printl oHash.x.y.x.point
'---Show oHash structure
printl oHash.ToString