|
adamwalker Apprentice
Joined: 12 Mar 2005 Posts: 195
|
Posted: Tue Nov 28, 2006 10:46 am
Variable sorting code. how do i fix it. |
Guys, any ideas whats wrong with this code... it will no longer do what its supposed to do :P
Code: |
DML=%countlist( @demonlist)
demonlist=""
#FORALL @demonorder {
#IF (%iskey( @DML, %i)) {
#LOOP %db( @DML, %i) {demonlist=%additem( %i, @demonlist)}
#DELKEY DML {%i}
}
}
#LOOPDB @DML {#LOOP %val {demonlist=%additem( %key, @demonlist)}}
#UNVAR BML |
what it should do is sort the contents of @Demonlist according to the order specified in @demonorder
it doesnt throw out a syntax error. it just seems to fall down on the %iskey and not get past that.
see http://forums.zuggsoft.com/phpbb/viewtopic.php?t=20773
for more details on what it was trying to do.. and the original code from Vijilante.
Thanks everyone |
|
|
|
The Raven Magician
Joined: 13 Oct 2000 Posts: 463
|
Posted: Tue Nov 28, 2006 6:22 pm |
Well... you #UNVAR BML, but don't you mean to #UNVAR DML? Looking at the original code, it appears you miscopied it somewhat. Here is my attempt to fix it, as well as updating it to use the new local CMUD vars and syntax.
Code: |
// First turn @demonlist into a database, with one entry for
// each unique monster and a count of how many there are of
// that monster. Then clear out @demonlist so we can
// reconstruct it in the proper order.
$dml=%countlist(@demonlist)
demonlist=""
// Loop through the bad monsters in the order we want them killed.
#FORALL @demonorder {
// If we find a bad monster in the room (in $dml)
#IF (%iskey($dml,%i)) {
// Add it to the list X times, once for each time we saw it
#LOOP %db($dml,%i) {
// We use %additem, because #ADDITEM won't add
// duplicate items.
demonlist=%additem(%i,@demonlist)
}
// Then remove this item from the $dml database now that we've
// added it back into the newly sorted @demonlist.
$dml=%delkey(%i,$dml)
}
}
// Finally, what about unsorted monsters that don't appear in
// @demonorder? We go through and add them at the end.
#LOOPDB $dml {
#LOOP %val {
demonlist=%additem(%key,@demonlist)
}
} |
I did not check this code, but I think it should do what you want. In particular, I may have the order of arguments wrong in %delkey. |
|
|
|
adamwalker Apprentice
Joined: 12 Mar 2005 Posts: 195
|
Posted: Tue Nov 28, 2006 7:10 pm |
hmmm, i tested it on a blank settings file and it worked. (the delkey needed to be changed tho)
however on my main file it wont work, can i be to do with other open databases? none of the names clash
Thankyou VERY much for your help raven. |
|
|
|
The Raven Magician
Joined: 13 Oct 2000 Posts: 463
|
Posted: Tue Nov 28, 2006 11:06 pm |
What do you mean by 'won't work'? What happens when it fails?
In particular, put these lines at the top and bottom of the code:
#ECHO @demonlist
#ECHO @demonorder
That way you can see what it does to it (if anything). |
|
|
|
adamwalker Apprentice
Joined: 12 Mar 2005 Posts: 195
|
Posted: Thu Nov 30, 2006 8:25 pm |
thanks again!
the following code
Code: |
#say top of code demonlist ~- @demonlist
#say top of code demonorder @demonorder
DML=%countlist( @demonlist)
demonlist=""
#FORALL @demonorder {
#IF (%iskey( @DML, %i)) {
#LOOP %db( @DML, %i) {demonlist=%additem( %i, @demonlist);#say Im looping}
#DELKEY DML {%i}
#say middle @demonlist
}
}
#LOOPDB @DML {#LOOP %val {demonlist=%additem( %key, @demonlist)}}
#UNVAR BML
#say bottom of code demonlist @demonlist
#say bottom of code demonorder @demonorder |
produced this output
top of code demonlist imp98625possession13360|haunt82901|possession102670
top of code demonorder possession|haunt|imp|gate|pestilence|parasite
bottom of code demonlist imp98625possession13360|haunt82901|possession102670
bottom of code demonorder possession|haunt|imp|gate|pestilence|parasite
notice the absence of the middle #say and the loop |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Thu Nov 30, 2006 8:57 pm |
Your demonlist doesn't have a | separating the imp and possession entries. You also still have #unvar BML rather than DML in there.
The main problem though, is that the string "imp98625" will never match the string "imp" that the %iskey part is searching for - that's why you never see an error. If you add a false command to that #if statement with "#echo iskey failed" you'll see that it's there that the script is going wrong.
Frankly, I'm astounded that that worked in zMUD. And unfortunately I have no idea if it's possible to somehow ignore the numbers temporarily while sorting. |
|
|
|
adamwalker Apprentice
Joined: 12 Mar 2005 Posts: 195
|
Posted: Thu Nov 30, 2006 9:25 pm |
ah the missing | is just a bady copy on my part. (plus ive fixed the dml bit, whoops!)
lookin at it, i entered the data without the numbers.... and it worked perfectly.
so how can i make it take into consideration the numbers in the sorting anyone.
worked a dream in zmud |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Thu Nov 30, 2006 9:29 pm |
I think this counts as an error on zMUD's part, though. I can't see how %iskey can return true for the string "imp" if it finds "imp29682962" in zMUD - the two aren't matches at all. It could cause some problems for other scripts :(
Do you use this variable-sorting script for anything else? If not, perhaps you could give us more information about what exactly you use the list for and maybe we can come up with something else that works. |
|
|
|
|
|