Hashtable Chilkat2 Perl Reference Documentation

Chilkat2::Hashtable

Current Version: 11.7.0

Markdown version of this page for AI tools (v 11.7.0) · all AI docs

Chilkat.Hashtable

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

Store values as strings or integers using straightforward add, replace, and lookup methods.

Fast key lookup

Check whether a key exists and retrieve its value without manually scanning a list or parsing text.

Add, replace, and remove

Maintain a mutable collection by adding new entries, replacing existing values, removing individual keys, or clearing the table.

Enumerate keys

Retrieve the set of keys when application logic needs to loop through the stored entries.

Query string helpers

Import URL query parameters into the table and serialize the table back to a query-string representation.

XML persistence

Save the hashtable to XML or reload it from XML when values need to be persisted or transferred in a simple structured format.

Common pattern: Use 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

use Chilkat2;

my $hashtable = Chilkat2::Hashtable->new;
# or set properties (and RaiseError) at creation:
# my $hashtable = Chilkat2::Hashtable->new(VerboseLogging => 1, RaiseError => 1);
my $hashtable = Chilkat2::Hashtable->new(Property => $value, ...);

Creates the object. The optional pairs set writable properties (and RaiseError) in order, as if set one by one; an unknown or read-only name croaks. use Chilkat2; loads every class; the Chilkat2/Hashtable.pm file holds only this class's documentation (perldoc Chilkat2::Hashtable).

Perl frees the object when its last reference goes (undef $hashtable, or the variable going out of scope); there is nothing to close. Objects returned by methods are independent and freed the same way. The class may be subclassed (use parent -norequire, 'Chilkat2::Hashtable';): an object is a blessed hash reference whose hash is free for the subclass's own fields.

my $boolVal = $hashtable->RaiseError;
$hashtable->RaiseError($boolVal);

When false (the default), a method that fails returns false (or undef), and the reason is in LastErrorText. When true, it dies with a Chilkat2::Error instead, as DBI does with its RaiseError. Objects returned by a method inherit the value of the object that returned them.

Errors

A Chilkat2::Error stringifies to the failed call's full LastErrorText, and has the methods text, class ('Chilkat2::Hashtable'), method (the method name) and object. Methods that look something up and find nothing return undef without dying (each such method says so), methods that answer a question (HasMember, IsUnlocked, ...) return a plain boolean, and property reads and writes never fail for Chilkat reasons.

Misuse always croaks, with your file and line, whatever RaiseError is: a wrong number of arguments, a reference or undef where a string or number is expected, an object of the wrong class, an out-of-range integer, a string containing a NUL character, a wide character in a byte string, or a write to a read-only property.

Strings are Perl character strings in both directions (length, regexes and :encoding layers see characters); binary data (BinData) is a byte string. Unlocking: call Chilkat2::Global->new->UnlockBundle('...') once at startup.

Properties

Count
# $intVal is an integer (read-only)
my $intVal = $hashtable->Count;
Introduced in version 9.5.0.97

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.

top
DebugLogFilePath
# $strVal is a string
my $strVal = $hashtable->DebugLogFilePath;
$hashtable->DebugLogFilePath($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.

More Information and Examples
top
LastErrorHtml
# $strVal is a string (read-only)
my $strVal = $hashtable->LastErrorHtml;

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.

top
LastErrorText
# $strVal is a string (read-only)
my $strVal = $hashtable->LastErrorText;

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.

top
LastErrorXml
# $strVal is a string (read-only)
my $strVal = $hashtable->LastErrorXml;

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.

top
LastMethodSuccess
# $boolVal is a boolean
my $boolVal = $hashtable->LastMethodSuccess;
$hashtable->LastMethodSuccess($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.

top
VerboseLogging
# $boolVal is a boolean
my $boolVal = $hashtable->VerboseLogging;
$hashtable->VerboseLogging($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.

top
Version
# $strVal is a string (read-only)
my $strVal = $hashtable->Version;

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

AddFromXmlSb
# $sbXml is a Chilkat2::StringBuilder
# $success is true or false
my $success = $hashtable->AddFromXmlSb($sbXml);
Introduced in version 9.5.0.64

Reads hashtable XML previously produced by ToXmlSb from the StringBuilder passed in $sbXml and adds the entries to this table.

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 true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
AddInt
# $key is a string
# $value is an integer
# $success is true or false
my $success = $hashtable->AddInt($key, $value);
Introduced in version 9.5.0.51

Adds an entry whose string key is $key and whose value is the integer $value. If the key already exists, its previous value is replaced.

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.

One entry per key: Adding the same key again replaces the existing value; it does not create a second entry. Replacing a value does not increase Count. Use LookupInt to retrieve an integer value.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
AddQueryParams
# $queryParams is a string
# $success is true or false
my $success = $hashtable->AddQueryParams($queryParams);
Introduced in version 9.5.0.62

Parses URL query parameters from $queryParams and adds them to the hashtable as string entries. Pass the query component in the following form:

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.

Repeated parameter names: A hashtable stores one value per key. It cannot preserve a query string containing several independent values with the same field name. Use a list-oriented parser when repeated names must remain distinct.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
AddStr
# $key is a string
# $value is a string
# $success is true or false
my $success = $hashtable->AddStr($key, $value);
Introduced in version 9.5.0.51

Adds an entry whose string key is $key and whose value is the string $value. If the key already exists, its previous value is replaced.

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.

One entry per key: Adding the same key again replaces the existing value; it does not create a second entry. Replacing a value does not increase Count. Use LookupStr to retrieve a string value.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
Clear
$hashtable->Clear();
Introduced in version 9.5.0.51

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.

Returns the object, so calls can be chained.

top
ClearWithNewCapacity
# $capacity is an integer
# $success is true or false
my $success = $hashtable->ClearWithNewCapacity($capacity);
Introduced in version 9.5.0.51

Removes every entry and reinitializes the hashtable with $capacity buckets. The initial default capacity of a new Hashtable object is 521.

Returns 1 on success and 0 if the table cannot be reinitialized.

TermMeaning
CountThe number of stored entries.
CapacityThe number of hash buckets, not a maximum entry count.
Performance note: More buckets can reduce collisions for a large table, but use more memory. Choosing roughly twice the expected number of entries and using a prime number are tuning guidelines, not correctness requirements.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

top
Contains
# $key is a string
# $flag is true or false
my $flag = $hashtable->Contains($key);
Introduced in version 9.5.0.51

Returns 1 if the hashtable contains the string key $key; otherwise returns 0. The result tests key existence regardless of the value stored for that key.

Useful before lookup: Call 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.

Never dies: false is an answer, not a failure.

top
ContainsIntKey
# $key is an integer
# $flag is true or false
my $flag = $hashtable->ContainsIntKey($key);
Introduced in version 9.5.0.64

Returns 1 if the hashtable contains the integer key $key; otherwise returns 0.

Key type: This method accepts an integer key. It is distinct from Contains, which tests for a string key, and from AddInt, whose key is a string and whose value is an integer.

Never dies: false is an answer, not a failure.

top
GetKeys
# $strTable is a Chilkat2::StringTable
# $success is true or false
my $success = $hashtable->GetKeys($strTable);
Introduced in version 9.5.0.62

Appends all hashtable key strings to the StringTable passed in $strTable. Existing strings already contained in $strTable are not cleared.

Returns 1 on success and 0 on failure.

Enumeration order: Hashtable keys may be returned in any order. Do not depend on insertion order or sorted order. Clear the destination StringTable first when it should contain only the current hashtable keys.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
LookupInt
# $key is a string
# $n is an integer
my $n = $hashtable->LookupInt($key);
Introduced in version 9.5.0.51

Returns the integer value associated with the string key $key. This method is intended for entries stored with AddInt.

If the key does not exist, the return value is 0.

Missing key versus zero: Because 0 is also a valid stored value, call Contains first when the application must distinguish a missing key from an entry containing zero.
top
LookupStr
# $key is a string
# $str is a string or undef
my $str = $hashtable->LookupStr($key);
Introduced in version 9.5.0.51

Returns the string value associated with the string key $key. This method is intended for entries stored as strings, including entries added by AddStr or AddQueryParams.

Check lookup success: The representation of a failed string-returning call varies by programming language and may be null, Nothing, or an empty string. An empty string may also be a legitimate value. Use Contains or LastMethodSuccess when the distinction matters.

Returns undef if not found: this is not an error, and it never dies, whatever RaiseError is.

top
Remove
# $key is a string
# $flag is true or false
my $flag = $hashtable->Remove($key);
Introduced in version 9.5.0.51

Removes the entry whose string key is $key.

  • Returns 1 if the key existed and the entry was removed.
  • Returns 0 if 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.

Never dies: false is an answer, not a failure.

top
ToQueryString
# $str is a string or undef
my $str = $hashtable->ToQueryString();
Introduced in version 9.5.0.92

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 ?.

Important limitations: Key order is not defined, and a hashtable contains only one value per key. Therefore the output is not suitable when parameter order is significant or repeated field names must be preserved. In language bindings where string-return failure is ambiguous, check LastMethodSuccess.

Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top
ToXmlSb
# $sbXml is a Chilkat2::StringBuilder
# $success is true or false
my $success = $hashtable->ToXmlSb($sbXml);
Introduced in version 9.5.0.64

Serializes the current hashtable to Chilkat's hashtable XML format and appends the XML to the StringBuilder passed in $sbXml. Existing content in the StringBuilder 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>
Ordering: Because hashtable enumeration order is not defined, do not rely on entries appearing in insertion order or use the serialized text as a canonical, byte-for-byte representation.

Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.

More Information and Examples
top