 |
Zhiroc Adept
Joined: 04 Feb 2005 Posts: 246
|
Posted: Fri Jan 11, 2008 6:02 pm
[2.18] Using the syntax "dbvar.key" doesn't work if key is a number |
Make a db variable with the package editor: a=b|1=2|4=5
Now do:
| Code: |
#SH @xxx.a ==> gives "b" (CORRECT)
#SH @xxx.1 ==> gives "a" (ERROR)
#SH @xxx.4 ==> blank (ERROR) |
Furthermore, doing:
will turn the db variable into a string list, where the last entry is just the 7.
#ADDKEY seems to do the right thing.
I wasn't aware of a restriction that the key must not be a number, and in any case, this corrupts the variable. |
|
|
|
 |
Vijilante SubAdmin

Joined: 18 Nov 2001 Posts: 5187
|
Posted: Fri Jan 11, 2008 9:57 pm |
Actually this is an old shortcut for working with string lists. Your line "#SH @xxx.1" should display "a=b", if it doesn't then that is a bug in this shortcut.
I seem to recall there is one tiny mention to this particular shortcut in the documentation, but I can't for the life of me find it. I know it was in there when I worked on the zMud help, and because I always thought this shortcut added confusion I never duplicated whatever sentence mentioned it all over the various entries for working with lists. |
|
_________________ The only good questions are the ones we have never answered before.
Search the Forums |
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Jan 11, 2008 10:55 pm |
Vijilante is correct. You cannot use the simple @var.key syntax if key is a number. If you need to use numeric keys in a database variable, use %db(@var,1) to access it.
|
|
|
|
 |
Zhiroc Adept
Joined: 04 Feb 2005 Posts: 246
|
Posted: Fri Jan 11, 2008 11:33 pm |
It doesn't display "a=b", just the "b", so that seems to be a bug at least. I guess there are two ways to think of it:
1) db vars are to be thought of as structs (in C terminology). Thus the member reference ".key" is a literal string that the programmer controls, and restricting to strings is OK
2) db vars are to thought of as associative arrays, and var.key is shortcut syntax, in which case you might not know what is being used as keys.
Obviously, 1 is more true. In fact, I think you can't use variables for keys anyways (though, you can use %n, which is how I caught it).
If I were to rate the usefulness of being able to have simpler syntax for associative arrays vs. losing the shortcut for stringlists, I'd probably take the former.
However, maybe a later enhancement might be to allow for syntax: @dbvar.{key} where key can be literal, a number, or a variable reference. |
|
|
|
 |
Fang Xianfu GURU

Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sat Jan 12, 2008 12:11 am |
You can actually use @{dbvar.@var} to dynamically reference variables.
I'd think of @var.key as a shortcut syntax only applicable when the key is a string, sort of like the Lua syntax var.key as opposed to var["key"] which can use anything as keys. %db is the equivalent of the latter. |
|
|
|
 |
|
|
|