Hash tables have benefits over variables and variables have benefits over hash tables. It all depends on what you are doing.

If you need a sorted hash table, you do have the option of saving it to a file and using /filter to sort it (include the original line number in /filter so you can reference the right item in the hash table). This works very well for something like storing scores for a game and then listing the top 10 (or bottom 10) scores. You can store the scores as normal in the hash table, then to show the top 10, you just save the table, /filter it, reference the first 10 items in the list in the hash table and you're done. Nice and easy. This example also puts in an automatic n/a if there isn't a score available (for example, if you only had 5 people with scores after scores are reset, then it would list a Top 10, but would show n/a for the last 5 scores until more people have scores).

For example, here's a way to see the Top 10 scores in a hash table filled with scores:

Code:
alias Top10 {
  hsave -n Scores Scores.txt
  filter -ffcuten 1 32 Scores.txt Scores.txt
  var %c = 1, %t = 10
  while (%c <= %t) {
    echo -a %c :: $iif($hget(Scores,$gettok($read(Scores.txt,nt,%c),1,32)).item,$v1,n/a)
    inc %c
  }
}


To use, fill a hash table called Scores up with nicks and scores (item = nick, data = score) and then use /Top10 . Obviously, you can adjust how it displays the scores depending how you want it to show up. Normally you'd want to have it on one line with some kind of nice formatting done to it. I just made something quick so you can see how it works.

How it works:

You save the hash table as data only (the scores, but not the nicks). The data will be in the same order as it was in the hash table, so item 1 in the hash table will be line 1 when saved.

Next, you filter that file so that it sorts it in numerical descending order (-ue) (remove -e if you want the bottom 10). You'll want to sort based on column one with space delimiter (-t 1 32) and you'll want to clear the file since you're overwriting the same file (-ff) with the sorted data (-c). Finally, you want to insert the original line number into the new file (-n).

Now that it's sorted, if you look at the new file, it will be in the format of:

Original_Line_# Score

Example:

8 3156
1 2345
4 104
3 46
2 32

Finally, you will want to pull up the nick for the scores when displaying them. The nick is the item in the table, so we would $read() the first line (8 3156 in the above example). We will use just the line number (8) from that, so we need $gettok() to get that line number. Then, we look it up in the hash table with $hget(Scores,8).item which will tell us the nick who is 8th in the table, but first in the scores list.

I know that's a long drawn out explanation, but it should answer any questions someone has about how it works so they can make it work the way they want with the data they have. And you can, of course, have a Top 5 or Top 100 or Bottom 5 or whatever else you want just by adjusting either the /filter (ascending vs. descending) or the loop (%t).

Just be aware that by doing it this way, if you somehow manage to change the hash table in the miliseconds it takes to filter and display the scores, it will show the wrong scores. So make sure that you have the scores displayed at a time when the hash table won't be updated (after or before a game, between questions in trivia, etc).

A note on using $read() -- If you're displaying many scores [lines of data], then you may want to consider other methods of reading the file (such as /fopen, /fread, /fclose commands) to speed things up and avoid multiple accesses of the file. There are other methods as well. For only showing 5-10 scores, it really doesn't matter all that much, but it's something to consider.

-----------------------------------
One last note. If you want to list a table in the same order the data was stored, you can actually use this same method. Just store your DATA field as $ctime DATA. By including $ctime, then you are sorting based on a number just like if it was a score. Use ascending or descending order depending on which way you want the table to be displayed (first to last or last to first). And make sure you adjust your $hget()'s so they ignore $1 in the DATA.


Invision Support
#Invision on irc.irchighway.net