Hashtable Lua Reference Documentation
chilkat.Hashtable
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
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
local chilkat = require("chilkat")
local hashtable = chilkat.Hashtable.new()Creates the object (chilkat.Hashtable() does the same). Lua's garbage collector frees it when it is no longer referenced; hashtable:dispose() frees it at once, and in Lua 5.4 and later local hashtable <close> = chilkat.Hashtable.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = hashtable:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (hashtable.verbose_logging = true) and methods use the colon (hashtable:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
Count
local intVal = hashtable.count
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
local strVal = hashtable.debug_log_file_path
hashtable.debug_log_file_path = 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
local strVal = hashtable.last_error_html
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
local strVal = hashtable.last_error_text
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
local strVal = hashtable.last_error_xml
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
local boolVal = hashtable.last_method_success
hashtable.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false 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.
VerboseLogging
local boolVal = hashtable.verbose_logging
hashtable.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = hashtable.version
Methods
AddFromXmlSb
-- ok is true, or nil and err on failure
local ok, err = hashtable:add_from_xml_sb(sb_xml)
Reads hashtable XML previously produced by ToXmlSb from the StringBuilder passed in and adds the entries to this table.sb_xml
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 true when the XML is parsed and imported successfully; otherwise returns false.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddInt
-- value is an integer
-- ok is true, or nil and err on failure
local ok, err = hashtable:add_int(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 true when the entry is added or replaced. Returns false if the operation cannot be completed, such as because memory could not be allocated.
Count. Use LookupInt to retrieve an integer value.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddQueryParams
-- ok is true, or nil and err on failure
local ok, err = hashtable:add_query_params(query_params)
Parses URL query parameters from and adds them to the hashtable as string entries. Pass the query component in the following form:query_params
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 true on success and false if the query parameters cannot be imported.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AddStr
-- value is a string
-- ok is true, or nil and err on failure
local ok, err = hashtable:add_str(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 true when the entry is added or replaced. Returns false if the operation cannot be completed, such as because memory could not be allocated.
Count. Use LookupStr to retrieve a string value.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Clear
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
-- ok is true, or nil and err on failure
local ok, err = hashtable:clear_with_new_capacity(capacity)
Removes every entry and reinitializes the hashtable with buckets. The initial default capacity of a new capacityHashtable object is 521.
Returns true on success and false 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 true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Contains
-- flag is true or false
local flag = hashtable:contains(key)
Returns true if the hashtable contains the string key ; otherwise returns keyfalse. 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.Returns true or false: an answer, not a success flag.
ContainsIntKey
-- flag is true or false
local flag = hashtable:contains_int_key(key)
Returns true if the hashtable contains the integer key ; otherwise returns keyfalse.
Contains, which tests for a string key, and from AddInt, whose key is a string and whose value is an integer.Returns true or false: an answer, not a success flag.
GetKeys
-- ok is true, or nil and err on failure
local ok, err = hashtable:get_keys(str_table)
Appends all hashtable key strings to the StringTable passed in . Existing strings already contained in str_table are not cleared.str_table
Returns true on success and false on failure.
StringTable first when it should contain only the current hashtable keys.Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
LookupInt
-- n is an integer
local n = hashtable:lookup_int(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
-- str is a string, or nil and err on failure
local str, err = hashtable:lookup_str(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 the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Remove
-- flag is true or false
local flag = hashtable:remove(key)
Removes the entry whose string key is .key
- Returns
trueif the key existed and the entry was removed. - Returns
falseif the key was not present.
A false 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.
Returns true or false: an answer, not a success flag.
ToQueryString
local str, err = hashtable:to_query_string()
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 the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
ToXmlSb
-- ok is true, or nil and err on failure
local ok, err = hashtable:to_xml_sb(sb_xml)
Serializes the current hashtable to Chilkat's hashtable XML format and appends the XML to the StringBuilder passed in . Existing content in the sb_xmlStringBuilder is preserved.
Returns true on success and false on failure. The generated XML can be loaded by AddFromXmlSb.
<hashtable> <e><k>key</k><v>value</v></e> </hashtable>
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.