Mintoris Forum

Author Topic: Hashtable  (Read 2319 times)


  • Sr. Member
  • ****
  • Posts: 126
  • If life throws a planet at you, pull your ripcord‼
« on: Apr 25, 2011, 04:31 AM »
A hashtable is a mechanism that allows programmers to save data without having to worry about variables and storage. Older languages call them hashtables. VB6 referred to them as a collection. Most newer languages have modified hashtables called maps, which are saved internally in a different format for faster value retrieval. Most hashtables can be saved to, and loaded from disk/storage.

Hashtables have 'entries'. Each entry consists of a Key and a Value. A key can be 'name' for example, and it's value can be 'Chuck' or 'Jesse', or whatever you want it to be. Once the entry is set, it is from then on referred to by its key. If you try to retrieve a value using a key that isn't in the table, a default value is returned. Also note that although a hashtable is kinda like an ini file, and it can be used like an ini file, it's saved into a text/binary hybrid format, so editing a saved hashtable isn't a good idea. If you need an ini file, consider using the ini include file by BillK.

wsHashTable is my implementation of a simple, single level hashtable. With it, you can set and get numeric and string values and load and save it to your Data directory. Although it is a good idea to include Mintoris Basic include files above the main executable code in your .bas files, it isn't strictly necessary for this case. Here is the syntax for using it:

Include the file:
Include "wsHashTable.bas"

Save a numeric value to the hashtable:
wsHTPut(key$, value)
wsHTPut("Age", 47)

Save a string value to the hashtable:
wsHTPut$(key$, value$)
wsHTPut$("Employer", "Walmart")  'not really…

Retrieve a numeric value:
wsHTGet(key$, defaultvalue)
age = wsHTGet("Age", -1)

Retrieve a string value:
wsHTGet$(key$, defaultvalue$)
name$ = wsHTGet$("name", "")

Save a hashtable:
if not wsHTSave("") then print "error saving hashtable"

Load a hashtable:
if not wsHTLoad("") then print "error loading hashtable"

At some point, I will most likely post the multi-level hashtable I've been working on. It's more like a Windows registry file, where the value of any key can be an embedded hashtable, giving you the ability to have nested hashtables. So you could access a value like this:

balance = wsHashTable("MyCheckbook/BankOfAmerica/Checking/Check1001/Balance", -1)

As you can see, keys for the embedded hashtables aren't really keys, but values themselves. For just such an occasion, there will be a mechanism for returning all the keys for an embedded hashtable, so you can retrieve all the financial institutions in MyCheckBook, and all the accounts for BankOfAmerica, or all the transactions for Checking account, or all the keys for a transaction.

There will also be a mechanism for returning a nested/embedded hashtable as a two dimensional array of keys and values. An example of this being used could be in this check book example where a transaction is extracted by a transaction number and the array returned would have entries for Check#, Date, Payee, Amount, Memo, and Balance. In this example, an sql database would be more appropriate, but you can see its usefulness. It could actually be used in place of an sql database for some applications.

I hope someone finds this useful, either in application or information. Happy coding‼

« Last Edit: Apr 25, 2011, 05:28 PM by Jesse »


  • Sr. Member
  • ****
  • Posts: 126
  • If life throws a planet at you, pull your ripcord‼
Re: Hashtable
« Reply #1 on: Apr 25, 2011, 05:29 PM »
I corrected a small typo in the wsHashTable.bas file. All is well now…