|
millisa Newbie
Joined: 17 Dec 2002 Posts: 7
|
Posted: Tue Jan 21, 2003 9:46 pm
Sort a Data Record Variable |
This shouldn't be hard but I'm not finding it (trying to search on 'sort' here is near impossible).
I've got a DataRecord Variable called Score:
Bob:1
Tom:5
Joe:12
Harry:1
Dick:3
I want to sort by the Number column.
If I do a #show @scores
I get something like this:
Bob|1|Tom|5|Joe|12|Harry|1|Dick|3
%sort doesn't seem to do what I want and I'm just not finding another simple way of doing this.
What the right way to sort a datarecord by numerical values?
(er, it would appear the nickname for 'Richard' isn't allowed, heh, pretend the **** is his name) |
|
|
|
Kjata GURU
Joined: 10 Oct 2000 Posts: 4379 Location: USA
|
Posted: Tue Jan 21, 2003 11:51 pm |
Here is an alias that sorts a record variable by values rather than keys. It only works for record variables where the values are numbers:
#ALIAS sortval {#VARIABLE templist "";#LOOPDB %1 {#VARIABLE templist %concat( @templist, "|", %repeat( "0", 10 - %len( %val)), %val, "=", %key)};#VARIABLE templist %delete( @templist, 1, 1);#VARIABLE templist %sort( @templist);#FORALL @templist {#SHOW %concat( %copy( %i, %pos( "=", %i) + 1, %len( %i) - %pos( "=", %i) + 1), ": ", %number( %copy( %i, 1, %pos( "=", %i) - 1)))};#UNVAR templist;#GAG}
Usage:
sortval recordVariable
Example:
sortval @myRecVar
Note: If a value is longer than 10 digits, the record variable may not be sorted correctly. This can be fixed by changing the 10 in the alias to a bigger number.
Kjata |
|
|
|
millisa Newbie
Joined: 17 Dec 2002 Posts: 7
|
Posted: Wed Jan 22, 2003 12:56 am |
Am going to give this a shot. As always, thanks oh' zmudmaster.
|
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Mon Apr 09, 2007 1:25 pm |
I bumped this up because I need to use something similar, but after it sorts it by values, I need to place the database keys into a variable (appropriately sorted). Using the above alias, how would I change the (#show blah blah blah) part to #additem dbase? If I input it directly, it disconnects me from the MUD. :\ Thanks.
Charneus |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Mon Apr 09, 2007 2:19 pm |
Once the alias arrives at the #show part, it's created a list in the format val=key|val=key and sorted it. All you need to do is use %right(%i,%pos("=",%i)) or something like that to extract the key name and then add that to a separate list.
|
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Tue Apr 10, 2007 6:23 am |
Well, tried that, but it didn't work. :\ I'll take a look deeper into it though, unless anyone else has any suggestions.
Charneus
EDIT:
Actually found it...
%right( %copy( %i, %pos( "=", %i) + 1, %len( %i) - %pos( "=", %i) + 1)) works. The reason your suggestion, Fang, didn't work, is because there wasn't a number to specifiy how far back to go from the right, so it was basically capturing the whole thing. At least, that's my theory. *shrug* Thanks, though! You made it possible for me to figure it out at least. :) |
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Tue Apr 10, 2007 10:34 am |
Try this
Code: |
#VAR sortvar {}
#ALIAS sortVal {#var sortvar %sort( %mss( %concat( "StrReverse(", %string( %expanddb( %1, "|")), ")"), "vbscript"), 1);
#var sortvar %mss( %concat( "StrReverse(", %string( @sortvar), ")"));
#var sortvar %subchar( @sortvar, "=|", %concat( %char( 30), %char( 29) ) )}
|
sortVal @Score
#showdb @sortvar |
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
Dharkael Enchanter
Joined: 05 Mar 2003 Posts: 593 Location: Canada
|
Posted: Thu Apr 19, 2007 9:07 pm New! and Improved???? |
I noticed a problem with the above alias because %sort() sorts alphanumerically it doesnt work if say you have the numbers 1 ,2 ,12
obviously 2 should come before 12 but because stringwise 12 comes before 2 it doesnt work out
so I made another alias thats a little (maybe a lot) more complicated but it will work as long as the numbers are less than 7 digits
and that number can be increased by making the %format('&7.f,%val) read %format('&12.f',%val) if for example you needed 12 digits
also this script doesn't require the use of JScript VBscript or anything except for zScript.
Code: |
#CLASS {DBNumericValSort}
#ALIAS SortByVal {
#var insort {}
#var postsort
#var insort %sort( %exec( "#loopdb %0 {#var insort %additem(%concat(%replace(%format('&7.f',%val),' ','0'),'=',%key),@insort)};@insort"))
#var insort %subchar( @insort, "=|", %concat( %char( 30), %char( 29)))
#loopdb @insort {#var postsort %addkey( @postsort, %val, %number( %key))}
}
#VAR postsort {} {}
#VAR insort {} {}
#CLASS 0 |
Usage:
SortByVal @presorted where @presorted is a db variable with numerical keys as values
#showdb @postsort should show the correct results |
|
_________________ -Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style." |
|
|
|
|
|
|
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
|
|