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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
rythin
Beginner


Joined: 12 Jul 2011
Posts: 13

PostPosted: Sun Nov 27, 2011 2:09 pm   

Sorting a "two-column" list.
 
Hi.
I have a quick question. I have a killstat list in which every field is "name,number" (So: "goblin,5|orc,13|dragon,2"). How can I sort that list into a descending order?

Thanks in advance.
Reply with quote
Daern
Sorcerer


Joined: 15 Apr 2011
Posts: 809

PostPosted: Sun Nov 27, 2011 5:44 pm   
 
Why not use a database variable? They're made for things like this... This code should convert it for you:
Code:
$temp = @killstat
killstat = %null
#forall $temp {
  $key = %word(%i, 1, ",")
  $val = %word(%i, 2, ",")
  #ADDKEY killstat $key $val
  }


Then whenever you add to the values in your triggers on killing mobs, you can do this instead:
Code:
#ADDKEY killstat %1 (%db(@killstat, %1) + 1) //assuming %1 is the key (name of the mob you just killed)


As for the sorting, this function should do it:
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <func name="sortdesc" copy="yes">
    <value>$temp = %1
#LOCAL $sorted
#WHILE %numitems($temp) {
  $maxindex = %isvalue($temp, %max(%dbvalues($temp)))
  #ADDKEY $sorted %dbkey($temp, $maxindex) %item($temp, $maxindex)
  #DELNITEM $temp $maxindex
  }
#RETURN $sorted</value>
  </func>
</cmud>


Then you can call it like this:
Code:
killstat = @sortdesc(@killstat)
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD 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