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
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 3:09 pm   

looping a database, and showing the list in order of the %key
 
Code:
#variable {test_list} {one=1|two=2|three=3|four=4|five=5}
#loopdb @test_list {#say %val "=" %key}


Gives me:

Current Output wrote:

2 = two
1 = one
3 = three
4 = four
5 = five


Which... Works, but I'd rather get it to do this:

Hopeful Output wrote:

1 = one
2 = two
3 = three
4 = four
5 = five


I'm sure its easy, just can't figure out a good way to do it. ^^ TIA
Reply with quote
Toxic
Adept


Joined: 27 May 2008
Posts: 299

PostPosted: Fri Jun 27, 2008 3:16 pm   
 
#LOCAL $temp;#LOOPDB @test_list {#ADDKEY $temp %val %key};#SHOWDB $temp

That should work. It reverses the val's and the keys of the dbvar and then they sort correctly automaticly.. This works with the example given, something tells me its a bit more complex than this tho.
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 3:23 pm   
 
Code:
#loopdb @test_list {#say %sort(%val) "=" %key}


output wrote:
2 = two
1 = one
3 = three
4 = four
5 = five


Almost... But why is the 2 first?
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 3:42 pm   
 
Hmm... Yea, that does work Toxic, but I'm trying to use it inside of an alias, and the output will be used in another window, not in a #say.
Code:
#alias {testorder} {#LOCAL $temp;#LOOPDB @test_list {#ADDKEY $temp %val %key};#WINDOW misc --== Order: $temp ~==--}


And that looks like that'd be juuuust fine. =D
misc wrote:
--== Order: 1=one|2=two|3=three|4=four|5=five ==--


The reason I was going with the previous statment, is because the alias would look similar to this:
Code:
#window misc --== Order ~==--;#loopdb @test_list {#window misc %sort(%val) "=" %key};#window misc --== ~=--


To get it to look like this:
misc wrote:
--== Order ==--
1 = one
2 = two
3 = three
4 = four
5 = five
--== ==--


Grainular.. I know... ^^
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Fri Jun 27, 2008 3:50 pm   
 
%sort(%val) won't do anything - %val is the value of the key for that particular iteration, which is usually a string or number and thus can't be sorted.

You can't sort dbvars. It's that simple - they're hashed now, which makes them much faster, but also means you can't sort them. Practically every other language does the same thing. It's misleading using dbvars with numbers as keys, because they seem to stay in order when they're hashed - if you use strings instead of numbers for keys (say, alice bert charlie) it's much more obvious that the variable isn't maintaining its order. There's almost certainly a way to do what you want without pfaffing about with key order, but to help you with that, we'd need to know the actual problem you're trying to solve. Anyway, with that said:

#local $temp
#loopdb @db {#additem $temp %key}
$temp=%sort($temp)
#forall $temp {#say Key: %i Val: %db(@db,%i)}
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Fri Jun 27, 2008 3:53 pm   
 
What follows is 100% untested

From this thread
Code:
<func name="NSort" language="Lua" id="1">
  <value>if zs.numparam < 1 then return "" end
local tbl = zs.param(1)
if type(tbl) ~= "table" then return tbl end
local errstr = "All Items in list given to NSort must be a number"
table.sort(tbl,function(a,b) return assert(tonumber(a),errstr)< assert(tonumber(b),errstr) end)
--table.sort(tbl)
return table.concat(tbl,"|")
</value>
</func>


That function should allow you to sort numerically.

then this code should do the trick
Code:
#for @NSort(%dbkeys(@test_list)) {#say %i = %db(@test_list,%i)}


The reason i didn't use CMUD's built in sort is that it sorts alphabetically not numerically which is what it appears you're looking for here.
No big deal for numbers 0-9 but if it gets higher than that then CMUD's %sort will fail you
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 4:03 pm   
 
yes Dharkael, sorting numerically! =D

However, I'm getting this error...

illegal token: = zs.param(1)
Reply with quote
Toxic
Adept


Joined: 27 May 2008
Posts: 299

PostPosted: Fri Jun 27, 2008 4:05 pm   
 
that doesn't do anything but show the same order as #SHOWDB @db.

sorting one|two|three|four|five is gonna give you five|four|one|three|two.

Im gonna assume the actual variable your doing this to is your order_list I built for you in the other post. SO in actuality its gonna look like this...

#var order_list {servant=1|female=2|priest=3}

Using Fangs example you would get...
Key: female Val: 2
Key: priest Val: 3
Key: servant Val: 1


Perhaps after doing what I showed you could replace the |'s with line breaks, not sure how that would work, it would also probaly require qouting each item in the string with replace at some point too, maybe subregex could be used to do this.

You would end up with $temp = "1=one"%cr"2=two"%cr"3=three" Again... Not sure that would work, just an idea.

Oo, I got Ninja'd... Twice. But Dhark is right after 9 cmud will screw you, didn't even think about that.


Last edited by Toxic on Fri Jun 27, 2008 4:06 pm; edited 1 time in total
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 4:06 pm   
 
went ahead and tried it, and it gives the the "local errstr" message... that means that its working, kinda? lol, tia!
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 4:13 pm   
 
Yea Toxic, same db var... hehe, your so smart... =]

The reasoning behind this, is that later on, when I start adding and removing mobs from the active script, I'd like to be able to reaarange the order in which the mobs are attacked, and this is just the start of all that, but for now, I'd just like to be able to see the order in which they are being handled. And... Sort it numerically, mostly because thats the order that I understand.

I know that the formatting is very grainular, but I didn't think that it would be this "difficult". I'm actually fine with the code that Toxic gave, just because other areas are VERY large, with a TON of mobs, and any longer, and I'd have to scroll up and down to see the list... Hahahahah!
Reply with quote
Toxic
Adept


Joined: 27 May 2008
Posts: 299

PostPosted: Fri Jun 27, 2008 4:16 pm   
 
One problem, my answer is going to mess up when the number of mobs goes over 9... for instance sorting

var = {1|5|3|6|7|9|10}

is gonna give you {1|10|3|5|6|7|9}

so you might want to look into sorting the $temp var I made with Dhark's answer.
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 4:25 pm   
 
Gar! There is always a catch! LoL!

Dhark... Don't suppose you know whats wrong with:
code wrote:
if zs.numparam < 1 then return "" end
local tbl = zs.param(1)
if type(tbl) ~= "table" then return tbl end
local errstr = "All Items in list given to NSort must be a number"
table.sort(tbl,function(a,b) return assert(tonumber(a),errstr)< assert(tonumber(b),errstr) end)
--table.sort(tbl)
return table.concat(tbl,"|")


Error message is that its an illegal token, dunno what that means... ~.~
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 4:28 pm   
 
Code:
#var {order_list} {servant=4|female=1|tormented=3|priest=5|old=2}
#alias {testorder} {#for @NSort(%dbkeys(@order_list)) {#say %i = %db(@order_list,%i)}}


gives me wrote:
servant = 4
female = 1
tormented = 3
priest = 5
old = 2


Also... My NSort looks like this:
Code:
if zs.numparam < 1 then return "" end
local tbl = zs.param(1)
if type(tbl) ~= "table" then return tbl end
local errstr = "All Items in list given to NSort must be a number"
--table.sort(tbl,function(a,b) return assert(tonumber(a),errstr)< assert(tonumber(b),errstr) end)
--table.sort(tbl)
return table.concat(tbl,"|")


Doing a syntax checker, doesn't come up with an error... Well, doesn't say anything really now.. However, when I do the alias TESTORDER it... "works"... thanks!
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Fri Jun 27, 2008 5:15 pm   
 
Actually, you *can* sort a database variable. You need to use the #SORT command:
Code:
#SORT order_list

So, in your original post example, do this:
Code:
#variable test_list {one=1|two=2|three=3|four=4}
#addkey test_list five 5
#sort @test_list
#loopdb @test_list {#say %val "=" %key}

The #ADDKEY is required because in your first line you use the #VAR command and that just assigns a string value to a variable (and sets it to Auto-Type). For the #SORT command to work, the variable must be set to a "Database record" type. The easiest way to do that is to create your database variable using #ADDKEY. Or you can just go into the Settings Editor and select the test_list variable and change it's type in the option box at the bottom to Database Variable.

Once the variable is set to a Database Variable type, then you can use the #SORT command to sort it by key. You can also do this in the Settings Editor by just clicking the Sort checkbox next to the variable name (once you change it to a database variable).
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 5:20 pm   
 
Yea, the original post, I noticed that I had created the database wrong, but I went ahead an silently "fixed" that... Hehe... =]

As far as the #SORT .... It isn't sorting it numerically by the %val as to what I'm aiming for... Unless I'm missing a step in there?
Reply with quote
Toxic
Adept


Joined: 27 May 2008
Posts: 299

PostPosted: Fri Jun 27, 2008 5:23 pm   
 
Now just do this...

Code:
#ALIAS testorder {#LOCAL $temp
#LOOPDB @order_list {#ADDKEY $temp %val %key}
#FORALL @NSort(%dbkeys($temp)) {#WIN misc %i = %db($temp,%i)}
}


This should give you...

1 = female
2 = old
3 = tormented
4 = servant
5 = priest

Of course thats completely untested but should work.
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 5:38 pm   
 
GAR! So close.... LoL!

Code:
#var {order_list} {ape=3|berzerker=2|female=8|golem=10|hunter=7|leader=11|lion=1|priest=9|pup=5|scout=6|therax=12|warrior=4}


Gave me:
Code:
10 = golem
11 = leader
12 = therax
1 = lion
2 = berzerker
3 = ape
4 = warrior
5 = pup
6 = scout
7 = hunter
8 = female
9 = priest
Reply with quote
Toxic
Adept


Joined: 27 May 2008
Posts: 299

PostPosted: Fri Jun 27, 2008 5:39 pm   
 
hmm then @nsort isnt working correctly, and since its written in lua, your on your own heh.
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Fri Jun 27, 2008 5:49 pm   
 
The problem Shaun is that you modified your NSort to do no sorting at all.

shaun.murray wrote:


Also... My NSort looks like this:
Code:
if zs.numparam < 1 then return "" end
local tbl = zs.param(1)
if type(tbl) ~= "table" then return tbl end
local errstr = "All Items in list given to NSort must be a number"
--table.sort(tbl,function(a,b) return assert(tonumber(a),errstr)< assert(tonumber(b),errstr) end)
--table.sort(tbl)
return table.concat(tbl,"|")


Code:

....
--table.sort(tbl,function(a,b) return assert(tonumber(a),errstr)< assert(tonumber(b),errstr) end)
--table.sort(tbl)
....


Both lines that do the sorting are commented out.
Code:
--table.sort(tbl,function(a,b) return assert(tonumber(a),errstr)< assert(tonumber(b),errstr) end)

should be
Code:
table.sort(tbl,function(a,b) return assert(tonumber(a),errstr)< assert(tonumber(b),errstr) end)


and the other table.sort(tbl) can be removed entirely if you wish
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
shaun.murray
Magician


Joined: 23 Jul 2005
Posts: 334
Location: Chicago

PostPosted: Fri Jun 27, 2008 5:55 pm   
 
ah, there we go! works great! =D thanks a ton Dharkael! ^^
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Fri Jun 27, 2008 7:48 pm   
 
True, #SORT is only alphabetical, not numerical. I hope to add other sorting options in the future.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4690
Location: Pensacola, FL, USA

PostPosted: Fri Jun 27, 2008 8:03 pm   
 
If your adding more sort types, could you do one where it would go by the nth word, right or left?
_________________
Discord: Shalimarwildcat
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Fri Jun 27, 2008 8:30 pm   
 
The only built-in type that I am considering is numeric sorting. And then some sort of method to allow a user-defined function to specify the sort order. But this is on the list for the future and not anything soon.
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