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
Valint
Wanderer


Joined: 12 Nov 2003
Posts: 70
Location: USA

PostPosted: Thu Dec 04, 2003 10:27 pm   

Sorting woes (numbers and databases)
 
Occasionally, I will want to sort numbers as numbers, rather than alphabetically as text, e.g., I want the sorted list to be 1|3|10 not 1|10|3. While I created an alias to do so, this seems a bit clunky to me; is there an easier way to do this?

#ALIAS sortnumlist {
#LOOP 1,%numitems( @{%1}) {
#IF (%isnumber( %item( @{%1}, %i))) {#VAR %1 %replaceitem( %repeat( "0", 8-%len( %item( @{%1}, %i)))%item( @{%1}, %i), %i, @{%1})}}
#VAR %1 %sort( @{%1})
#LOOP 1,%numitems( @{%1}) {
#IF (%isnumber( %item( @{%1}, %i))) {#VAR %1 %replaceitem( %eval( %item( @{%1}, %i)), %i, @{%1})}}}

Also I have an occasional need (well, more just to clean up the way it looks) to sort a database record, either by %key or (for those databases with unique values as well) %val. Again, I created an alias to do so, but I'm curious as to whether there's an easier way.

#ALIAS sortdb {
#VAR sortdbsourcedb @{%1}
#VAR sortdbkeylist %null
#LOOPDB @sortdbsourcedb {#ADDITEM sortdbkeylist {%key}}
#VAR sortdbkeylist %sort(@sortdbkeylist)
#VAR %1 %null
#FORALL @sortdbkeylist {#ADDKEY %i {%i} {%db( @sortdbsourcedb, "%i")}}}

#ALIAS revsortdb {
#VAR revsorttempdb %null
#LOOPDB @{%1} {#ADDKEY revsorttempdb {%val} {%key}}
#IF (%numkeys( @{%1})!=%numkeys( @revsorttempdb)) {
#SAY Error! Not all values in source variable are unique.} {
sortdb revsorttempdb
#VAR %1 %null
#LOOPDB @revsorttempdb {#ADDKEY %1 {%val} {%key}}}}
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Fri Dec 05, 2003 2:43 am   
 
No. Your aliases might possibly be improved (I didn't evaluate them) but there's no built-in method, so there isn't any easier way than doing it yourself.
Reply with quote
megamog75
Enchanter


Joined: 20 Nov 2002
Posts: 627
Location: USA

PostPosted: Mon Dec 08, 2003 6:13 pm   
 
That's great, Here is the one I use.

#ALIAS ReOrg {ReOrgN=%1;#var ReOrg %additem(%min(%replace(@{@ReOrgN},"|",",")),@ReOrg);#deli @ReOrgN %min(%replace(@{@ReOrgN},"|",","));#if @{@ReOrgN}="" {#forall @ReOrg {#say %i};#var @ReOrgN @ReOrg;ReOrg=""} {ReOrg @ReOrgN}}
Reply with quote
sp000n
Novice


Joined: 04 Jul 2001
Posts: 32

PostPosted: Tue Dec 23, 2003 9:17 pm   
 
Valint, is your alias actually working? It doesn't seem to for me. megamog75's crashes Zmud every time (b6.66).
Reply with quote
Talahaski
Enchanter


Joined: 10 Oct 2000
Posts: 656
Location: USA

PostPosted: Tue Dec 23, 2003 9:56 pm   
 
If you know your max value, you could always just add that many leading zeros then do a character sort.

1,10,3 becomes 0000001,0000010,0000003 which will sort correctly to 0000001,0000003,0000010.

I'm not sure if this syntax will work. I use this stragegy when working with SQL, but the code should be fairly simple in zmud.

Something like this should pad the zeros in front?

@var=%right(%eval('000000'+@var),6)
Reply with quote
Valint
Wanderer


Joined: 12 Nov 2003
Posts: 70
Location: USA

PostPosted: Tue Dec 30, 2003 8:54 pm   
 
> Valint, is your alias actually working?

Yes. The alias does, however, have some added linefeeds for readability, so you will have to edit it slightly to remove those extra spaces before cutting and pasting.
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