mIRC Scripting

mIRC: Hash Tables

mIRC Hash Tables are similar in a way to a Microsoft Excel Grid (like SQL Database). Imagine a table where the first column is the item and each column after it is your input which can be altered in many ways and manipulated as you wish.

Why use Hash Tables?

Variables, Plain Text Files, and INI Files are all stored on you computer’s Hard Drive. Hash Tables are stored on the RAM (Random Access Memory) (uses hashing search instead of linear). Your system can access the RAM much faster than you hard drive (depends on the hard drive speed), thus making the hash tables the quickest way to store information.

Creating a Hash Table

Before you can use a hash table you will need to create one:

/hmake -s <name> <N>
  1. -- -s switch just makes mIRC disply the results in the status window.
  2. -- <name> is the actual name of the script.
  3. -- <N> is a way of making the hash table faster by setting the item limit. For example if you think you will need to enter 100 items then N will be 10. This doesn't mean you can't put 101 items in it, it just deal with the speed of the hash table.

Lets make a Channel OP Command table:

/hmake -s MyChanOP 10

Results:
* Made hash table 'MyChanOP' (10)

This created a table called "MyChanOP" with no data.

MyChanOP

Add Items and Data to the table

To add data to the table we will use the following syntax:

/hadd -smbczuN <name> <item> <data>

-- -s switch is the same as the /hmake.
-- -uN just means the item will get unset after N seconds
-- -b means you are adding a &binvar data
-- <name> is the name of the Hash Table
-- <item> is the item you will refer to in the future
-- <data> is the value assigned to the item

Continue with the example from before:

/hadd -s MyChanOP OP1 Mike
/hadd -s MyChanOP OP2 James
/hadd -s MyChanOP OP3 Dave
/hadd -s MyChanOP OP4 Anna

Results:

* Added item 'OP1' to hash table 'MyChanOP'
* Added item 'OP2' to hash table 'MyChanOP'
* Added item 'OP3' to hash table 'MyChanOP'
* Added item 'OP4' to hash table 'MyChanOP'

This created 4 items in our original table. Now the table looks somthing like this:

MyChanOP
OP1 Mike
OP2 James
OP3 Dave
OP4 Anna

 

Delete Items and Data from the table

To deleted data from the table we will use the following syntax:

/hdel -sw <name> <item>

-- -s the same as before
-- -w makes the item be Wild Matched
-- <name> is the name of the table
-- <item> is the name of the item

Continue with the example from before:

/hdel -s MyChanOP OP4

Results:

* Deleted item 'OP4' from hash table 'MyChanOP'

This deleted the item the matched the name of "OP4". Now the table looks somthing like this:

MyChanOP
OP1 Mike
OP2 James
OP3 Dave

Getting Items' Data from the table

The following will return the name of the table if it exists.

$hget(<Name>)

To get the value of the item you will use the following:

$hget(<Name>,<Item>)

The following is used to get the name of the item without really knowing the name.
If you set 0 for <N> then it will return the total number of item in the table.

$hget(<Name>,<N>).Item

This is the same as above except this lets you access the value of the item directly without knowing the item's name.
If you set 0 for <N> then it will return the total number of data in the table.

$hget(<Name>,<N>).Data

Example:

This trigger will check if the table exists. If it does it will loop through all the items and op all the users it finds. (Although using a while loop is not quite efficient, we will use it for example purposes)

ON *:TEXT:!OP:#MyChan: {
  if ($hget(MyChanOP) == MyChanOP) {
    var %x = 1
    while (%x <= $hget(MyChanOP,0).Item) {
      MODE $chan +o $hget(MyChanOP,%x).Data
      inc %x
    }
  }

  else {
    MSG $chan $chr(3) $+ 2,0 * Error: MyChanOP Table Was Not Found
  }
}

Saving the Hash Table

Hash Tables are a great way of storing data. However there is a small draw back, everything that is stored in a hash table is only temporary, meaning it will get deleted when you exit mIRC. This is the same as when you shut down the computer, the entire ram gets cleared.

/hsave -sbniauo <name> <filename>
  • -- -b makes it as binary files. $cr and $lf are preserved when saving as binary files.
  • -- -n saves the files as data only.
  • -- -a append to an existing file instead of overwriting it.
  • -- -o overwriting an existing file.
  • -- -u switch forces it to include the unset items.
  • -- -i switch treats the file as an ini file. You can specify an optional section name after the filename.\

Example:

We will use the following code to save the table before quitting mIRC.

/hsave -os MyChanOP SaveOP.txt

Results

* Saved hash table 'MyChanOP' to 'SaveOP.txt'

Loading a Hash Table

To load a hash table we use the following syntax:
Please note: before you can use this command, you must first create the table.

/hload -sbni <name> <filename>
  • -- -b makes it as binary files. $cr and $lf are preserved when saving as binary files.
  • -- -n saves the files as data only.
  • -- -i switch treats the file as an ini file. You can specify an optional section name after the filename.

Deleting the Hash Table

To delete a Hash Table we use the following syntax:

/hfree -sw <name>

Example:

/hfree -s MyChanOP

Results

* Freed hash table 'MyChanOP' (10)