Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
panor
Newbie


Joined: 13 Aug 2002
Posts: 8

PostPosted: Sun Jul 13, 2003 2:51 am   

how can i sort some variables from high to low
 
basicly im counting death's from death info on avatar mud.

i see
(%W) killed by *

output
#var death%1 1

now how do i develope a list of say top 10 and list them in order from most deaths to worst. also i put the death word at the start of the trigger becuase im using player names themselves as variables elsewhere so i'd like to somehow subtract that word from the output counter that shows people.

also while i siad it. im finding putting all my variables in the one main folder is very crowding. can i direct a variable to be created inside a class folder?

thanks for any answers and time taken to write them

PaNoR, Dragon warrior
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Sun Jul 13, 2003 1:31 pm   
 
The method you are using for storing the deaths is not the best. Using a record variable would take care of this easily and you would only have one variable containing everything.

In a record variable, you have a list of key-value pairs. For this case, the name of the person would be the keys while the amount of deaths would be the corresponding values. Here's the trigger for keeping track fo the deaths using a record variable:
#TRIGGER {(%w) killed by *} {#ADDKEY deaths {%1} {%eval(%db(@deaths, "%1") + 1)}}

For more information, try reading the help entries for the #ADDKEY command and the %db function. Basically what the trigger does is to use the #ADDKEY command to add a new key-value pair to the record variable. However, if the specified key already exists, the #ADDKEY command replaces its value with the new one given in the command. For this reason, the %db command is used, to extract the current value of the that key (if the key doesn't yet exist, it will return 0) and add one to it to form the new value.

Now, sorting this variable is a bit complicated. It requires temporarily converting all of the values into keys and vice-verse, plus padding out the values with 0's at the beggining. Here is the script that would sort the variable:
#VAR temp ""
#LOOPDB @deaths {#VAR temp %concat(@temp, %repeat("0", 10 - %len(%val)), %val, %char(30), %key, "|")}
#VAR temp %leftback(@temp, 1)
#VAR temp %sort(@temp)
#VAR temp %replace(@temp, "|", %char(29))
#VAR deaths ""
#LOOPDB @temp {#VAR deaths %concat(@deaths, %val, %char(30), %eval(%key), %char(29))}
#VAR deaths %leftback(@deaths, 1)

As for displaying it, the %expanddb function makes this simple:
#SHOW %expanddb(@deaths, %crlf, ": ")
Reply with quote
panor
Newbie


Joined: 13 Aug 2002
Posts: 8

PostPosted: Mon Jul 14, 2003 12:29 am   
 
wow thanks. thought i'd wait to reply untill i looked up all the commands you've used. i've also been playing with what you've written. myself i dislike using something i dont understand cuss i can't fix it or change it if needed. only two problems that i tried and failed to fix

i can't get it to display, hehe. its also ordering the database seemly (i just typed #ec @temp and looked at it) in order from least to most. i'd like it to order from say the top 20, most to least. ie

displaying with alias
gtell panor was killed 10 times
gtell blah1 was killed 9 times
gtell thisguy was killed 5 times

and when it gets to 20 players. it just stops showing. seems i would re-sort the list for order it this way. then have it loop to 20 and stop. easy to say. im weak on loop commands and i have no idea how to withdraw the info from the database var.
can you explain why it puts 000000001 and counts up from that number? is this to do with the leftback command?

Thanks for your help. im close to getting my weak non-program brain around it.

Sean
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Mon Jul 14, 2003 12:47 am   
 
The %leftback is meant to strip off the extra |. It is a small programming shortcut to eliminate an #IF within the loop and thereby go faster.

As for changes to make the sort order highest to lowest, and provide better display...change the stuff in red.
#VAR temp ""
#LOOPDB @deaths {#VAR temp %concat(@temp, %repeat("0", 10 - %len(%val)), %val, %char(30), %key, "|")}
#VAR temp %leftback(@temp, 1)
#VAR temp %sort(@temp,1)
#IF (%numitems(@temp)>20) {#VAR temp {%delete(@temp,%eval(%pos(%item(@temp,21),@temp)-1,%len(@temp))}}
#VAR temp %replace(@temp, "|", %char(29))
#VAR deaths ""
#LOOPDB @temp {#ECHO gtell %val was killed %eval(%key) times}

No further display code is needed. The green #ECHO is there for testing, when you see everything is working right go ahead and remove it to preform the actual group tells.
Reply with quote
panor
Newbie


Joined: 13 Aug 2002
Posts: 8

PostPosted: Mon Jul 14, 2003 1:32 am   
 
perfect thats got things in order. i'd like to display it with an alias command rather then have it display on each death. im planing to stay logged on and just biuld a list. looking at who has died the most whenever i want to with an alias command

i'll figure this out im sure shortly. it displayed once in echo and now it wont. think the if as just reached its limit and i need to reset the var. i'll figure it out. this is nice though, i can really use this type of code on some other counters. thanks bunches
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Mon Jul 14, 2003 2:24 am   
 
The trigger that records deaths should not contain all the sorting and display script. Do the sorting and display as a seperate alias, or sort in 1 alias and display in another.
Reply with quote
panor
Newbie


Joined: 13 Aug 2002
Posts: 8

PostPosted: Mon Jul 14, 2003 2:34 am   
 
yup just realized that. was hoping to resond before you did [8D]

thanks for the help think im ok now. i think
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net