$hget

From Scriptwiki
Jump to: navigation, search

As you can give $hget different parameters, they will be listed one by one:

  • Returns name of a hash table if it exists, or returns the name of the Nth hash table.
$hget(name/N)[.size]

The size property returns the N size of table, as specified in /hmake.

  • Returns the data associated with an item in the specified hash table.
$hget(name/N, item)[.unset]

The unset property returns the time remaining before an item is unset.

  • Assigns the contents of an item to a &binvar.
$hget(name/N, item, &binvar)
  • This allows you to reference the table as an index from 0 to N, in order to look up the Nth item in the table.
$hget(name/N, N).item

If N is zero, it returns the total number of items in the table.

You can also reference the Nth data value directly with $hget().data, but note, that this method is provided as a convenience, it is not an efficient way to use the hash table.

Example

At first, we need a hashtable for all examples, so lets make one:

; have a nice alias called makehash for it
alias makehash {
 ; here we actually make the hashtable
 hmake testhash 100
 ; now lets put data in it
 hadd testhash Dana Dana@staff.quakenet.org
 hadd testhash user1 user1@random.host
 hadd testhash user2 user2@another.host
}
echo -a $hget(testhash) has the size: $hget(testhash).size

This example will echo makehash has the size: 100.

echo -a $hget(testhash,user1)

This one will echo user1@random.host as it's the data connected to user1.

The following, more complex example will loop through the entire table and echo everything:

; lets make a little alias called showhash. At the end, it will look like /showhash <name>
alias showhash {
 ; echo the name and a kind of "table header"
 echo -a $1
 echo -a item => data
 var %i = 1
 ; lets loop through all items. $hget($1,0).item will return the total amount of items.
 while (%i <= $hget($1,0).item) {
  echo -a $hget($1,%i).item => $hget($1,%i).data
  ; increase looping-variable
  inc %i
 } 
}

Using /showhash testhash will echo:

user1 => user1@random.host
user2 => user2@another.host
Dana => Dana@staff.quakenet.org