mIRC Home    About    Download    Register    News    Help

Topic Options
#264796 - 12/01/19 03:18 PM Help with converting to a hashtable
chillout Offline
Pikka bird

Registered: 12/01/19
Posts: 18
I have a script thats writes the ip/hostmask (in 192.168.1.1 or hostmask-in.addr.com format) to a text file but want to tune this script up with hashtables insteed of writing and reading the addresses to/from a .txt file. Can somebody help me with this? Thanks in advance!
Code:
#retrieving the last added line:
var %f = *!*@ $+ $read(dns.txt,$lines(dns.txt))

Code:
#write only the hostmask or ip address:
on *:DNS: { .write dns.txt $raddress } 

Top
#264799 - 13/01/19 02:55 AM Re: Help with converting to a hashtable [Re: chillout]
maroon Offline
Hoopy frood

Registered: 12/01/04
Posts: 1141
Have a look at the wikichip page for hash tables for an overview of how they work. The linked page for /hmake also has some things to watch out for when translating to hashtables.

Unless you use buckets=1, which can be slower for large number of items, items will appear to be listed in a scrambled order. If you need to keep track of the last line, and don't care so much about the order of the prior items, you can have a special item named LASTLINE which you write the new stuff to. But immediately prior to overwriting LASTLINE, you would first add the existing LASTLINE data to another item in the table. I've not tested this, but it should give you a headstart.

Code:
#write only the hostmask or ip address:
on *:DNS: {
if ($hget(dns,line_num)) hadd DNS $v1 $hget(DNS,lastline)
hinc -m DNS line_num
hadd DNS lastline $raddress
} 


This gives you a table full of items which are integers plus another pair of items named line_num and lastline.

Code:
#retrieving the last added line:
var %f = *!*@ $+ $hget(DNS,lastline)


Note that ONLY if buckets=1, your last added line will always be $hget(DNS,1). (Which isn't the same as the 1st item in the linked list of items => $hget(DNS,1).item)

Using a higher bucket count has the advantage of making it much quicker to search for the 5th item among 10 thousand items.

Since hashtables aren't saved like global variables are, you might wish to have a timer which /hsave's your table to disk at time intervals or after N items are added to the table.

Top
#264814 - 13/01/19 10:08 PM Re: Help with converting to a hashtable [Re: maroon]
chillout Offline
Pikka bird

Registered: 12/01/19
Posts: 18
Thank for your time and information maroon, I realize the wiki for the hashtables,thats why I posted the question here since I don't want to use buckets or items and got confused. Ive noticed the hsave, hfree etc. commands so that part is gonna be oke. I made a little change to your suggestions and testing it now so I will give feedback in a few days. Again thank you.

Top
#264821 - 15/01/19 02:25 PM Re: Help with converting to a hashtable [Re: maroon]
chillout Offline
Pikka bird

Registered: 12/01/19
Posts: 18
Ok I tested the code and it seems to working perfect, it adds the 'lastline' with the $raddress without buckets or items (cause i dont need that) and overwrites by every trigger the lastline with the dns or hostmask in the hashtable, thats all i need, I don't need to store all the addresses. So I changed:
Code:
#write only the hostmask or ip address:
on *:DNS: { hadd DNS lastline $raddress }

Don't know if that's the correct way but it works, the other code is untouched, Thank you maroon!

Top