Hashtable Tcl Reference Documentation
Hashtable
Current Version: 11.5.0
Chilkat.Hashtable
Store values as strings or integers using straightforward add, replace,
and lookup methods.
Check whether a key exists and retrieve its value without manually
scanning a list or parsing text.
Maintain a mutable collection by adding new entries, replacing existing
values, removing individual keys, or clearing the table.
Retrieve the set of keys when application logic needs to loop through
the stored entries.
Import URL query parameters into the table and serialize the table back
to a query-string representation.
Save the hashtable to XML or reload it from XML when values need to be
persisted or transferred in a simple structured format.
For an extended overview, see
Hashtable Class Overview.
Store, look up, remove, enumerate, import, and serialize key/value pairs.
Chilkat.Hashtable provides a simple key/value collection for
storing string and integer values by key. It can add or replace entries,
look up values, test whether keys exist, remove entries, count items,
enumerate keys, import URL query parameters, serialize values back to a
query string, and save or load the table in XML format.
String and integer values
Fast key lookup
Add, replace, and remove
Enumerate keys
Query string helpers
XML persistence
Hashtable when application code needs a small mutable
key/value table, especially for lookup tables, parsed query parameters, or
simple string/integer settings. Add values with AddStr or
AddInt, retrieve them with LookupStr or
LookupInt, and use XML or query-string methods when the table
needs to be saved, loaded, or converted.
Object Creation
# 'this' is not a keyword in Tcl. It can freely be used as a variable name. set this [new CkHashtable]
Properties
Count
set intVal [CkHashtable_get_Count $this]
The number of entries currently stored in the hashtable.
Each key contributes one entry regardless of whether its value is a string or an integer. Adding a new key increases Count; replacing the value of an existing key does not. Removing an entry decreases it, and Clear resets it to 0.
DebugLogFilePath
# ckStr is a CkString
CkHashtable_get_DebugLogFilePath $this $ckStr
set strVal [CkHashtable_get_debugLogFilePath $this]
CkHashtable_put_DebugLogFilePath $this $strVal
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
LastErrorHtml
# ckStr is a CkString
CkHashtable_get_LastErrorHtml $this $ckStr
set strVal [CkHashtable_get_lastErrorHtml $this]
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
# ckStr is a CkString
CkHashtable_get_LastErrorText $this $ckStr
set strVal [CkHashtable_get_lastErrorText $this]
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
# ckStr is a CkString
CkHashtable_get_LastErrorXml $this $ckStr
set strVal [CkHashtable_get_lastErrorXml $this]
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
set boolVal [CkHashtable_get_LastMethodSuccess $this]
CkHashtable_put_LastMethodSuccess $this $boolVal
Indicates the success or failure of the most recent method call: 1 means success, 0 means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
Utf8
set boolVal [CkHashtable_get_Utf8 $this]
CkHashtable_put_Utf8 $this $boolVal
When set to 1, all string arguments and return values are interpreted as UTF-8 strings. When set to 0, they are interpreted as ANSI strings.
In Chilkat v11.0.0 and later, the default value is 1. Before v11.0.0, it was 0.
VerboseLogging
set boolVal [CkHashtable_get_VerboseLogging $this]
CkHashtable_put_VerboseLogging $this $boolVal
If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
# ckStr is a CkString
CkHashtable_get_Version $this $ckStr
set strVal [CkHashtable_get_version $this]
Methods
AddFromXmlSb
set status [CkHashtable_AddFromXmlSb $this $sbXml]
Reads hashtable XML previously produced by ToXmlSb from the StringBuilder passed in and adds the entries to this table.sbXml
The operation merges with the existing table: keys not present in the XML remain, and an imported entry replaces the current value for the same key. Call Clear first when the XML should completely replace the current contents.
Returns 1 when the XML is parsed and imported successfully; otherwise returns 0.
Returns 1 for success, 0 for failure.
AddInt
# value is an integer
set status [CkHashtable_AddInt $this $key $value]
Adds an entry whose string key is and whose value is the integer key. If the key already exists, its previous value is replaced.value
Returns 1 when the entry is added or replaced. Returns 0 if the operation cannot be completed, such as because memory could not be allocated.
Count. Use LookupInt to retrieve an integer value.Returns 1 for success, 0 for failure.
topAddQueryParams
set status [CkHashtable_AddQueryParams $this $queryParams]
Parses URL query parameters from and adds them to the hashtable as string entries. Pass the query component in the following form:queryParams
field1=value1&field2=value2&field3=value3
Each value is expected to be URL encoded and is URL decoded before being stored. If an imported key already exists, its previous value is replaced. Returns 1 on success and 0 if the query parameters cannot be imported.
Returns 1 for success, 0 for failure.
AddStr
# value is a string
set status [CkHashtable_AddStr $this $key $value]
Adds an entry whose string key is and whose value is the string key. If the key already exists, its previous value is replaced.value
Returns 1 when the entry is added or replaced. Returns 0 if the operation cannot be completed, such as because memory could not be allocated.
Count. Use LookupStr to retrieve a string value.Returns 1 for success, 0 for failure.
topClear
Removes every entry from the hashtable. After this call, Count is 0.
The current hash-table capacity is retained. To clear the entries and also select a different bucket capacity, call ClearWithNewCapacity.
ClearWithNewCapacity
set status [CkHashtable_ClearWithNewCapacity $this $capacity]
Removes every entry and reinitializes the hashtable with buckets. The initial default capacity of a new capacityHashtable object is 521.
Returns 1 on success and 0 if the table cannot be reinitialized.
| Term | Meaning |
|---|---|
Count | The number of stored entries. |
| Capacity | The number of hash buckets, not a maximum entry count. |
Returns 1 for success, 0 for failure.
topContains
set retBool [CkHashtable_Contains $this $key]
Returns 1 if the hashtable contains the string key ; otherwise returns key0. The result tests key existence regardless of the value stored for that key.
Contains when a lookup result could be ambiguous. For example, LookupInt returns 0 both for a missing key and for an entry whose value is actually zero.ContainsIntKey
set retBool [CkHashtable_ContainsIntKey $this $key]
Returns 1 if the hashtable contains the integer key ; otherwise returns key0.
Contains, which tests for a string key, and from AddInt, whose key is a string and whose value is an integer.GetKeys
set status [CkHashtable_GetKeys $this $strTable]
Appends all hashtable key strings to the StringTable passed in . Existing strings already contained in strTable are not cleared.strTable
Returns 1 on success and 0 on failure.
StringTable first when it should contain only the current hashtable keys.Returns 1 for success, 0 for failure.
LookupInt
set retInt [CkHashtable_LookupInt $this $key]
Returns the integer value associated with the string key . This method is intended for entries stored with keyAddInt.
If the key does not exist, the return value is 0.
0 is also a valid stored value, call Contains first when the application must distinguish a missing key from an entry containing zero.LookupStr
# outStr is a CkString (output)
set status [CkHashtable_LookupStr $this $key $outStr]
set retStr [CkHashtable_lookupStr $this $key]
Returns the string value associated with the string key . This method is intended for entries stored as strings, including entries added by keyAddStr or AddQueryParams.
null, Nothing, or an empty string. An empty string may also be a legitimate value. Use Contains or LastMethodSuccess when the distinction matters.Returns 1 for success, 0 for failure.
topRemove
set retBool [CkHashtable_Remove $this $key]
Removes the entry whose string key is .key
- Returns
1if the key existed and the entry was removed. - Returns
0if the key was not present.
A 0 result caused by an absent key is a normal lookup outcome, not an indication that the hashtable was damaged. Removing an existing entry decreases Count by one.
ToQueryString
set status [CkHashtable_ToQueryString $this $outStr]
set retStr [CkHashtable_toQueryString $this]
Serializes the hashtable as a URL query string in the following form:
key1=value1&key2=value2&key3=value3
Each value is URL encoded. The returned string does not include a leading ?.
LastMethodSuccess.Returns 1 for success, 0 for failure.
ToXmlSb
set status [CkHashtable_ToXmlSb $this $sbXml]
Serializes the current hashtable to Chilkat's hashtable XML format and appends the XML to the StringBuilder passed in . Existing content in the sbXmlStringBuilder is preserved.
Returns 1 on success and 0 on failure. The generated XML can be loaded by AddFromXmlSb.
<hashtable> <e><k>key</k><v>value</v></e> </hashtable>
Returns 1 for success, 0 for failure.