Originally Posted By: maroon
$hfind(voting,vote*,0,w) would give the total number of vote options, which can be your counter to cycle through all the choices. If you have a lot of choices and want just the winner, you use $hget(voting,$hfind(voting,vote*,%counterdown,w)) to get each choice's total, and grab them if they're the highest so far.

If your total vote choices+votecount will be few enough to fit within mIRC's 4150 line length (which must also include the entire equation checking them), you can let sorttok do most of the work. With $base() you can pad the vote totals to all have 5 digits, then append the vote name to them. This lets you do an alphabetical sort to get them in order, after which you can grab the tokens and display them.

Assuming you're using $hfind() above to loop through your vote choices and at each of the Nth choice you've put the values into %choice_text and %choice_votes. Before looping you've used "var %results" to set that local var to $null.

var %result %result $base(%choice_votes,10,10,5) $+ x $+ %choice_text

that assumes no more than 99999 votes for each option, and creates a text label like 00012:VoteA, and adds it as a space-separated token after any similar item already added to %result, until %result is a long string containing a 'word' for each of the choices. The ':' can be anything, as long as it's not a character that would be the same as the last letter of any of the choice texts.

So after looping the contents of %result would be like 00012:VoteA 00008:VoteB etc

$sorttok(%result,32,ar) does a reverse alphabetic sort, that puts the greater text value at the front of the list, which would be your winner.

If you want to display all results, you can use $numtok(%result,32) to get the total number of options, then $gettok(%result,%count,32) to access each item. Since you padded the votes to 5 digits, you wouldn't want to display them like 00002, you could use remove the extra zeroes several ways.

if you pull the token 00012:VoteA into %xyz, then the label would be $mid(%xyz,7) and the votes would be in $left(%xyz,5). You could use $base again to translate the number to+from base 10 without using the 5 padding length, or you could take advantage of a quirk in how several functions handle text labels that begin with numbers. If you use only 1 parameter, and feed it to $calc() or $abs(), it effectively strips off anything beginning with the first non-text character. These both would do the same thing, with $abs() also stripping any "-" from the front:

//echo -a $calc(00012:voteA) $abs(00012:voteA)

Now that you've parsed the label and vote count into 2 variables, you can display 1-or-all any way you wish.






Unfortunately, Im having a hard time understanding and applying this to my Code. I now have the vote automated so that it opens, accepts votes, closes and allows another vote to be opened. I would like for it to find the winner of the vote between the 4 options based on the numerical value. I found this on an old post but Im having trouble with it's accuracy. when I tell it to display the winner, it just posts an option rather than the one with the highest numerical value.

Originally Posted By: Riamus2

// Sort table in descending order into a temp file (this can also be done in a custom window)
hsave -n table file.tmp
filter -ffcuten 2 32 file.tmp file.tmp

// Get the Nth top value
echo -a $hget(table,$gettok($read(file.tmp,N),1,32)).item