|
DemonLlama Beginner
Joined: 03 Nov 2002 Posts: 27 Location: USA
|
Posted: Tue Sep 07, 2010 7:59 pm
[3.25] Numbers as keys |
I'm sure I'm doing something boneheaded, but here goes:
Setup:
#ADDKEY testdb 1 test1
#ADDKEY testdb 2 test2
#ADDKEY testdb 13 test13
Expected:
#SAY %db(@testdb,1) = "test1"
#SAY %db(@testdb,2) = "test2"
#SAY %db(@testdb,3) = null
#SAY %db(@testdb,13) = "test13"
Actual:
#SAY %db(@testdb,1) = "test1"
#SAY %db(@testdb,2) = "test2"
#SAY %db(@testdb,3) = "test13"
#SAY %db(@testdb,13) = "test13"
Forgive me if my search-fu on the forums weren't up to par; I did try to search for this first.
I tried searching through the helpfiles, but I didn't see anything about using numbers as keys. After talking it over with Charneus, it appears that it considers the number in the %db reference as an index value if it can't find the number as an actual key. I've tried all sorts of adding and then referencing the items using %string(1), "1", etc, to try to force it as a string, but to no avail.
I'm re-doing that part of the script by adding an "n" in front of the number (which adds a lot of juggling), but figured I would report it in case it's a bug. |
|
_________________ ~DemonLlama~ |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Tue Sep 07, 2010 10:17 pm |
The behavior that you mentioned is as designed. As you said, if you use a numeric value and there is no key for that number, then CMUD treats the number as an index into the nth item of the table.
To determine if a key to a table is value, you need to use the %iskey function:
#SAY %iskey(@testdb,3) = 0
#SAY %iskey(@testdb,13) = 3
There is no way for CMUD to return a "null" value for %db(@testdb,3) if there are 3+ items in the table. |
|
|
|
dbosst Apprentice
Joined: 15 Jun 2010 Posts: 121
|
Posted: Tue Sep 07, 2010 11:00 pm |
I was also surprised when I discovered this "feature": http://forums.zuggsoft.com/forums/viewtopic.php?p=158749
It makes it a real pain if your first column are numbers... and you want to sort a temporary variable:
Here is an idea on how to sort it if you want to sort it by numbers, maybe you can use the idea in your own script (need to make creative use of %concat and %number):
You can try a hack like this:
Code: |
$tmp_sort1=%dbkeys($zonelength)
#forall $tmp_sort1 {
#additem $tmp_sort2 %concat(" ",%replace(%format("&7.f",%i)," ","0"))
}
$tmp_sort1=%sort($tmp_sort2,1)
#forall $tmp_sort1 {
#additem $steps_sort %number(%i)
}
|
will sort $zonelength which is a database variable whose keys are numbers (in descending order), and put it in $steps_sort
it creates a temporary database with the keys as virtual strings since I prepend a space and format with leading zeros, but %sort will still work on it
and if you wanted to access this temporary database using the actual key name (which is a number), in your case though you probably don't need the format if you don't need to sort, that way you can access keys with numbers using just concat like %db(@dbvar, %concat(" ",thenumber)) |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Tue Sep 07, 2010 11:38 pm |
Hmm, thinking about this a bit more: would it make sense to force people to use %item to return the nth item and only allow %db to look at the actual key values? That might be doable if it makes sense to people.
Still won't effect the problem using the syntax @var.3 where it will either fetch the key "3" or the 3rd item in a list. |
|
|
|
DemonLlama Beginner
Joined: 03 Nov 2002 Posts: 27 Location: USA
|
Posted: Wed Sep 08, 2010 12:30 am |
@Zugg:
I think it would make more sense, personally, in that the behavior either works as-is or fails, but I guess my original bug report can be rephrased thusly: this isn't documented in an easy-to-read place for someone just starting to play around with db variables :P |
|
_________________ ~DemonLlama~ |
|
|
|
GeneralStonewall Magician
Joined: 02 Feb 2004 Posts: 364 Location: USA
|
Posted: Wed Sep 08, 2010 12:31 am |
Zugg wrote: |
Hmm, thinking about this a bit more: would it make sense to force people to use %item to return the nth item and only allow %db to look at the actual key values? That might be doable if it makes sense to people.
Still won't effect the problem using the syntax @var.3 where it will either fetch the key "3" or the 3rd item in a list. |
Yes, that would make much more sense, imo. As for the @var.3 issue, that's a toughie, probably leave as is, I suppose. |
|
|
|
|
|
|
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
|
|