Jump to content

Hash Tables


Tutorial

Recommended Posts

Hash Tables

 

Description:

 

Hash tables allow you to efficiently store large amounts of information which can be quickly referenced and retrieved later on.

 

Advantages:

 

Ability to store items by 2 parameters : name and item name.

 

Easy access to data through the topic and item names.

 

Ability to save to .ini files, and load .ini files to them.

 

Can set an item to have binary value and work like a &binvar.

 

Disadvantages:

 

No trivial sort command.

 

Storage:

 

The data is stored in the memory.

 

Initializing a hash table:

 

/hmake name N

 

Creates a new hash table with that name, and N items. (N is optional)

 

Adding data:

 

/had name item data

 

Adds data to the specified hash tables’ item.

 

/had name item &binvar

 

Copies the contents of a &binvar to the hash tables’ item.

 

Deleting data:

 

/hdel name item

 

Deletes the specified hash table’s item.

 

/hfree name

 

Destroys the hash table.

 

Retrieving data:

 

Set %data $hget(name,item)

 

Returns the data associated with the item in the specified hash table.

 

$hget(name,item,&binvar)

 

Puts the binary data in the item into the &binvar.

 

Searching for data:

 

Set %hash $hget(N)

 

Gets the name of the Nth hash table.

 

Set %item $hget(name,N).item

 

Gets the name of the Nth item in the hash table.

 

Set %item $hfind(name/N, *wild*match*, N, w)

 

Searches for the Nth item that matches the wild match expression. (There are more options besides w to set the type of the match text).

 

Set %item $hfind(name/N, *wild*match*, N, w).data

 

Searches the data of the hash tables’ items and returns the Nth item which has matching data.

 

Converting to files :

 

/hsave –i name file.ini section

 

Will save the hash table to an .ini file under the specified section. (note that .ini files can’t contain [ ] characters in the item names or section name).

 

/hload –i name file.ini section

 

Will load an .ini files’ section into the specified hash table. This way you can save many hash tables to the same file. (hload does not create hash tables, so you’ll have to make sure it’s already created).

 

/hsave name file.txt

 

Will save the hash table to a regular text file. You lose the ability to save multiple hash tables, but lose the limitation of not using the [ ] characters as well. (items will be followed by their data, separated by an end of line character).

 

/hload name file.txt

 

Will load a .txt file to the hash table. (hload does not create hash tables, so you’ll have to make sure it’s already created).

 

Retrieving size:

 

Set %size $hget(name).size

 

Returns the size of the hash table (number of items).

 

Example:

 

Load the example to the remotes, and right click on a channel.

 

menu channel {

  Hash tables Store: {

    var %hashname = $+(mychanneldata,$chan)

    hadd -m %hashname topic $chan($chan).topic

    hadd %hashname mode $chan($chan).mode

    if ($chan($chan).key) hadd %hashname $chan key $v1

  }

  Hash tables Show: {

    var %hashname = $+(mychanneldata,$chan)

    if ($hget(%hashname)) {

      echo -a Recorded channel topic: $hget(%hashname,topic)

      if ($hget(%hashname).size = 3) echo -a I've stored 3 items, so there must be a key: $hget(%hashname,key)

      echo -a Last stored modes were: $hget(%hashname,mode)

    }

  }

  Hash tables Remove :{

    var %hashname = $+(mychanneldata,$chan)

    if ($hget(%hashname)) hfree %hashname

  }

  Hash tables Remove all:{

    hfree -w mychanneldata*

  }

}

Link to comment
Share on other sites

×
×
  • Create New...