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
worgrider
Newbie


Joined: 08 Dec 2004
Posts: 2

PostPosted: Fri Feb 04, 2005 6:43 pm   

Sort Data Record Variable
 
I store information in a Data Record Variable, and then I have an alias that does a LOOPDB against the Variable and shows me the data.

How can I get it to Sort the data before it displays?
Reply with quote
Aarlot
Adept


Joined: 30 Dec 2003
Posts: 226

PostPosted: Fri Feb 04, 2005 8:11 pm   
 
It depends on what you want to sort it by. If you're sorting by value, I have a script I can post here when I get home.
_________________
Everyone is entitled to their beliefs - until they die. Then only the truth matters.
Reply with quote
worgrider
Newbie


Joined: 08 Dec 2004
Posts: 2

PostPosted: Fri Feb 04, 2005 9:01 pm   
 
I'd like to sort it by the key.
Reply with quote
Full Throttle
Wanderer


Joined: 07 Dec 2004
Posts: 65

PostPosted: Fri Feb 04, 2005 10:42 pm   
 
Key Sort

Code:
#ALIAS {keysort} {
#VAR input_db %1
#VAR key_list %null
#LOOPDB @input_db {#ADDITEM key_list {%key}}
#VAR key_list %sort(@key_list)
#VAR output_db %null
#FORALL @key_list {#ADDKEY output_db {%i} {%db(@input_db,"%i")}}
#SHOWDB @output_db
#UNV key_list
#UNV input_db
#UNV output_db}


Val Sort

Code:
#ALIAS {valsort} {
#VAR input_db %1
#VAR val_db %null
#LOOPDB @input_db {
#VAR val_db %concat(@val_db,%repeat("0",10-%len(%val)),%val,%char(30),%key,"|")}
#VAR val_db %leftback(@val_db,1)
#VAR val_db %sort(@val_db)
#VAR val_db %replace(@val_db,"|",%char(29))
#VAR output_db %null
#LOOPDB @val_db {#VAR output_db %concat(@output_db,%val,%char(30),%eval(%key),%char(29))}
#VAR output_db %leftback(@output_db,1)
#SHOWDB @output_db
#UNV val_db
#UNV input_db
#UNV output_db}


Example

Code:
#VAR test_db %null
#ADDKEY test_db {Eric} {8}
#ADDKEY test_db {Brad} {5}
#ADDKEY test_db {Alice} {5}
#ADDKEY test_db {David} {1}
#ADDKEY test_db {Cindy} {4}
#ECHO Database
#SHOWDB @test_db
#ECHO Key Sort
keysort @test_db
#ECHO Val Sort
valsort @test_db
#UNV test_db


Database
Eric: 8
Brad: 5
Alice: 5
David: 1
Cindy: 4

Key Sort
Alice: 5
Brad: 5
Cindy: 4
David: 1
Eric: 8

Val Sort
David: 1
Cindy: 4
Alice: 5
Brad: 5
Eric: 8
Reply with quote
Davos
Adept


Joined: 30 Jan 2003
Posts: 228
Location: USA

PostPosted: Sun Feb 26, 2006 5:30 pm   
 
I have a question about the valsort code...
when I try to run a dbvar thru it like

key1=Test string|key2=A string|key3=The String

I want it to sort the values... but its not....
_________________
The Seaworthy
Reply with quote
Davos
Adept


Joined: 30 Jan 2003
Posts: 228
Location: USA

PostPosted: Tue Feb 28, 2006 1:52 pm   
 
*bump*
_________________
The Seaworthy
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Tue Feb 28, 2006 7:48 pm   
 
Try keeping a list of your keys in a string list as you add and delete them. You can sort the list and use that sorted list to index your data record variable. Works wonders for me, and it's incredibly fast.

Code:
#VAR testdb {}
#VAR testdb_keys {}
#ALIAS addrec {#addkey testdb {%1} {%2};#additem testdb_keys {%1}}
#ALIAS delrec {#delkey testdb {%1};#delitem testdb_keys {%1}}
#ALIAS sortdb {testdb_keys = %sort(@testdb_keys)}
#ALIAS showdb {sortdb;#forall @testdb_keys {#show {%i -- %db(@testdb, %i)}}}
Reply with quote
Davos
Adept


Joined: 30 Jan 2003
Posts: 228
Location: USA

PostPosted: Wed Mar 01, 2006 3:06 am   
 
Im trying to sort off the Value of a dbvar not the key... and in this particular dbvar some of the value's could be the same word/string so referencing with another list is next to impossible. I just want to sort a dbvar by the value field when the value field is non numeric.. Gotta be some efficient way to do it.
_________________
The Seaworthy
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Fri Sep 26, 2008 3:11 pm   
 
1st, sorry to revive an old post.
I'm using the below script and I was wondering, is there a way to REVERSE the sort and of course keeping the name in alpha order?

Rather than
David: 1
Cindy: 4
Alice: 5
Brad: 5
Eric: 8

Get
Eric: 8
Alice: 5
Brad: 5
Cindy: 4
David: 1

%push won't help because it would just take the current, and move it and reverse the alpha...
Eric: 8
Brad: 5
Alice: 5

Cindy: 4
David: 1
Full Throttle wrote:
Code:
#ALIAS {valsort} {
#VAR input_db %1
#VAR val_db %null
#LOOPDB @input_db {
#VAR val_db %concat(@val_db,%repeat("0",10-%len(%val)),%val,%char(30),%key,"|")}
#VAR val_db %leftback(@val_db,1)
#VAR val_db %sort(@val_db)
#VAR val_db %replace(@val_db,"|",%char(29))
#VAR output_db %null
#LOOPDB @val_db {#VAR output_db %concat(@output_db,%val,%char(30),%eval(%key),%char(29))}
#VAR output_db %leftback(@output_db,1)
#SHOWDB @output_db
#UNV val_db
#UNV input_db
#UNV output_db}
Reply with quote
ralgith
Sorcerer


Joined: 13 Jan 2006
Posts: 715

PostPosted: Thu Oct 02, 2008 12:32 am   
 
Vitae wrote:
Code:
#VAR val_db %sort(@val_db)


*Cough* You didn't read the help file for %sort, did you? :P
Code:
#VAR val_db %sort(@val_db,1)
_________________
CrossOver: Windows Compatibility on Mac and Linux CMUD Advocate
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Thu Oct 02, 2008 12:54 am   
 
Readings for losers!
yeah, sorry, missed that part :-)
*grovel*
_________________
http://www.Aardwolf.com
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