Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD Beta Forum
dbosst
Apprentice


Joined: 15 Jun 2010
Posts: 121

PostPosted: Mon Jul 12, 2010 7:37 pm   

3.22 %db functionality with keys as numbers
 
I noticed a bug or maybe it is a feature (if it's a feature its undocumented), that you can do this:

%db(@dbvariable, 5) to get the value of the 5th key in @dbvariable (as opposed to a key with the name "5")

This only works if the key is not valid, in otherwords it will return the 5th key only if there isn't another key with keyname "5"

Has anyone else known about this?

I am reporting this since it caused a lot of debugging when I was I using numbers in the actual keys for a database variable, ie compare these two aliases:
(for demonstration of the bug, this appends a item to a key, but the keys are all unique...)

Code:

tmptmp="";#forall "b|a" {
  #show key 1 value before adding: %db(@tmptmp,1)
  #addkey tmptmp %i %additem(%i,%db(@tmptmp,%i))
  #echo loop %i, value tmptmp: @tmptmp
}


produces as expected the correct value of @tmptmp: (but notice the key1 value should be null really all the time, since there is no key with name "1")

key 1 value before adding:
loop b, value tmptmp: b=b
key 1 value before adding: b
loop a, value tmptmp: b=b|a=a

You get into trouble if you use keys with numbers:

Code:

tmptmp="";#forall "50|1" {
  #show key1 value: %db(@tmptmp,%string(1))
  #addkey tmptmp %i %additem(%i,%db(@tmptmp,%i))
  #echo loop %i, value tmptmp: @tmptmp
}


key 1 value before adding:
loop 50, value tmptmp: 50=50
key 1 value before adding: 50
loop 1, value tmptmp: 50=50|1="50|1"

different behavior since it looked up the index 1 instead of the key named "1" in the %db inside the %additem

regarding the bold condition at the top: its even weirder if you look at it like this, after you executed that second code above:

Code:

#echo 1 : %db(@tmptmp,1), 2: %db(@tmptmp,2), 5: %db(@tmptmp,50)


produces:
1 : 50|1, 2: 50|1, 5: 50

this makes it a bit more difficult to work with database variables with keys as numbers (like a variable holding a sorted cost list, where the keys are the costs, and the values are the names)... have to do something fairly esoteric:
like doing %if(%iskey(@tmptmp,%i),%db(@tmptmp,%i)) instead of normal %db
Reply with quote
Zugg
MASTER


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

PostPosted: Mon Jul 12, 2010 10:17 pm   
 
Quote:
This only works if the key is not valid, in otherwords it will return the 5th key only if there isn't another key with keyname "5"

Yes, that is correct and that is how it works. Unfortunately, there is no way around this. I tried to make it so that if you actually had KEYS that were strings of numbers, that it would take precedence over the numeric index value. The problem is that older versions allows you to do stuff like %item(@db,5) to get the 5th item in a database variable, so I still needed to support that for compatibility. Also, when CMUD converts string list arrays into database variables, it uses the numeric index of each item as the key in the new database variable.

So yes, you are correct that you have to be really careful when mixing these two concepts (numeric key vs numeric index)
Reply with quote
GeneralStonewall
Magician


Joined: 02 Feb 2004
Posts: 364
Location: USA

PostPosted: Mon Jul 12, 2010 11:26 pm   
 
So why not leave that compatibility in %item instead of carrying it over to %db? It doesn't quite make sense to me that the 'key' argument of %db would ever be treated as a numeric index value. I'm not sure how this is a backward compatibility issue, either, as %item( @db, index) still works in 3.22. So then, what's the point of making it a special case of %db?
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Tue Jul 13, 2010 12:08 am   
 
I believe it was added to %db to allow support for such things as %db(@rec,"a.3.bc"). When Zugg first talked about changing the underlying structures for record and list variables, I thought it would be good to have the same system handling the dot notation "@rec.a.3.bc" and the db function. Everyone that commented seemed to agree.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Zugg
MASTER


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

PostPosted: Tue Jul 13, 2010 5:50 pm   
 
Yep, Vijilante is correct. %db supports an entire "path" query.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD Beta Forum 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