Instead of looping to find the hash table data that matches the nick given, you can use $hfind with a wildmatch:

if (!? * iswm $1-2) msg # $hget(counter,$hfind(counter,$2 *,1,w).data)

As for a rank system, you can use the following alias:

alias _rank {
hsave -on counter tmp
filter -teucff $1 32 tmp tmp
var %i = $2,%x,%n = $1
while (%i <= $3) && ($read(tmp,%i)) {
tokenize 32 $ifmatch
%x = $addtok(%x,%i $+ . $1 - $($ $+ %n $ $+ $calc(1+%n),2),44)
inc %i
}
return $replace(%x,$chr(44),$chr(44) $+ $chr(32))
}

Then use $_rank(N,start,num) where N is either 2,4 or 6 (2 to rank by lines, 4 for words, 6 for letters), start is the rank to start with, and num is number of entries to return from the starting rank. So to get a top 10 based on lines you can use $_rank(2,1,10)