|
Yodous Apprentice
Joined: 23 Jun 2004 Posts: 105 Location: Poland
|
Posted: Thu Oct 08, 2009 12:34 pm
#LOOPDB problem with %sort() and database records |
Hi.
I have some variable of type 'database record' to store some shortcuts for attacking mobs. The variable name is @dbObjectShortcut.
When i write:
Code: |
#SA @dbObjectShortcut |
I get:
Code: |
sk=skavena|sl=sluz|b=banite|c=czerwonowlosego hobgoblina|d=demona|g=goblina|st=strzyge|h=hobgoblina|k=krasnozwierza|m=mutanta|n=nieumarlego|o=orka|p=psa|r=reptiliona|s=szkielet|t=trolla|z=zwierzoczleka|ban=banite|skav=skavena|gob=goblina|kos=kosciotrupa|ni=nietoperza|zw=zwierze |
But when i write:
Code: |
#SA %sort(@dbObjectShortcut) |
I get:
Code: |
b=banite|ban=banite|c=czerwonowlosego hobgoblina|d=demona|g=goblina|gob=goblina|h=hobgoblina|k=krasnozwierza|kos=kosciotrupa|m=mutanta|n=nieumarlego|ni=nietoperza|o=orka|p=psa|r=reptiliona|s=szkielet|sk=skavena|skav=skavena|sl=sluz|st=strzyge|t=trolla|z=zwierzoczleka|zw=zwierze |
Everything seems to be ok, but if I want to loop over this variable in some order and trying to use:
Code: |
#LOOPDB %sort(@dbObjectShortcut) {
#SA %key => %val
}
|
I got the same output if there wasn't any %sort() function.
My questions for Zugg are:
1) can You please include %sort() function in #LOOPDB command so the output will be in numeric form
2) can You please implement dictionary sort in other function. This is used to first compare length of the words for example this words are in dictionary order: a, b, c, ao, ba, asd, qwe, asdwqe etc
Now, to achive %sort in #LOOPDB i have to do something like:
Code: |
#FO %sort(%dbkeys(@dbObjectShortcut)) {
#SA %i => %db(@dbObjectShortcut, %i)
}
|
Best regards
Yodous |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Oct 08, 2009 4:31 pm |
User-defined sorting is planned for the future. The speed benefits of using a hashed database table far exceeded the lack of key sorting for most people in CMUD.
In most programming languages that use "database variables" (or associative arrays in Perl and PHP, tables in Lua, etc) there is no defined order for the keys. When using hash tables for performance, the order of keys is always undefined. CMUD is no different.
The reason this wasn't a problem in zMUD was because zMUD stored database variables as simple strings and didn't use any hash table, which made database variable manipulation in zMUD very slow.
If you go into the Settings Editor and select your @dbObjectShortcut variable, there should be a checkbox option on the right to set this variable to Sorted.
Also, the #SORT command will enable this option on a variable and sort it:
#SORT @dbObjectShortcut
The %sort function simply returns a *string* value of the sorted database variable (in key=value|key=value format). %sort doesn't actually cause the internal hash table to be sorted.
Since #LOOPDB is working with the internal hash table for speed, it doesn't see the result of %sort.
If you want to *force* this to work with %sort and #LOOPDB, the %dbkeys method works, but you can also use %string to force CMUD to convert the hash table into a string:
#LOOPDB %string(%sort(@dbObjectShortcut) {...}
This should prevent #LOOPDB from using the internal hash table and forces it to convert the database variable into a string value.
However, this will be much slower than using the #SORT command to set the Sorted property on the variable itself. |
|
|
|
Yodous Apprentice
Joined: 23 Jun 2004 Posts: 105 Location: Poland
|
Posted: Fri Oct 09, 2009 12:30 am |
Ok.
I understand.
Thx for repley Zugg :)
Best Regads |
|
|
|
|
|
|
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
|
|