mIRC Home    About    Download    Register    News    Help

Print Thread
need some help with hash tables #165310 25/11/06 06:37 AM
Joined: Jun 2006
Posts: 79
B
BanJirian Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Jun 2006
Posts: 79
I just wanna check who is the highest point .. But dont know how to do

alias yourpoint { .msg #trivia $_pc $todo }
alias _pc {
if (!$hget(point,$nick)) return --
if ($hget(point,$nick)) return $hget(point,$nick)
}
;--
alias todo {
if $hget(point,$nick) is highest { return u`re #1 }
}

or is it posibble to show all the ranks .. yes ? how ? thanks smile

Re: need some help with hash tables #165311 25/11/06 10:20 AM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
You could cycle through all the entries:
Code:
alias findmax {
  var %n = $hget(point,0).item, %max = 0, %maxnick
  while (%n) {
    if ($hget(point,%n).data > %max) { 
      %max = $v1 | %maxnick = $hget(point,%n) 
    }
    dec %n 
  }
  return %maxnick %max
}


or you could add an entry that records that information and update it every time you change a value:
Code:
alias init {
hmake points

hadd points @max | ;; Using '@' since it can't be in a nickname
;; any other init stuff
}
alias update_points {
  var %nick = $$1, %points = $$2
  hadd points %nick %points
  if (%points > $gettok($hget(points,@max),2,32)) {
    hadd points @max %nick %points 
  }
}


Or you could just have some global variables which store %max_points and %max_nick and do the same as the above. I dislike global vars, though smile


Sais
Re: need some help with hash tables #165312 25/11/06 11:30 AM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Ideally, you should keep track of the max value and update it each time you add/update a nick in the table, as Sais suggested. If for some reason you don't want that though, a method of getting the max values that is faster than looping through $hget().item (which can be quite slow with large tables) is described here.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: need some help with hash tables #165313 25/11/06 04:35 PM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
That's the basic way I sort hash tables as well. It's quick and efficient. smile


Invision Support
#Invision on irc.irchighway.net
Re: need some help with hash tables #165314 25/11/06 08:01 PM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Code:
alias findmax {
  var %n = $hget(point,0).item, %max = 0, %maxnick
  while (%n) {
    if ($hget(point,%n).data > %max) { 
      %max = $v1 | %maxnick = $hget(point,%n)[color:blue].item[/color] 
    }
    dec %n 
  }
  return %maxnick %max
}

Re: need some help with hash tables #165315 26/11/06 10:13 AM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Thanks.

I could have sworn that $hget(hash,N) gave the item name (like almost any of the other identifiers that take name/N as an accessor param)


Sais
Re: need some help with hash tables #165316 26/11/06 09:51 PM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Use .item with that.

$hget(Name,N).item

EDIT: Hm... You probably already realized that from DaveC's post. I should look up further to see what was already posted before replying. smile

Last edited by Riamus2; 26/11/06 09:52 PM.

Invision Support
#Invision on irc.irchighway.net
Re: need some help with hash tables #165317 26/11/06 10:12 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
It doesn't give the item name because you could have an item named as a number. Requiring .item/.data when using the $hget(table, N) format removes the possibility for conflict.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: need some help with hash tables #165318 26/11/06 11:51 PM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Good point. I vaguely remember realising that at some stage. Apparently I forgot again!


Sais