|
Anrok Apprentice
Joined: 19 Nov 2010 Posts: 119
|
Posted: Sat Nov 20, 2010 4:50 pm
Database variables and key switching using variables. |
Hi again everyone, i found a slight ? inconsistency ? when using variables to switch between my databases and database records
For example if i have a direct variable @name=test and @key=test2 and i have a database variable called @test which has values and keys test1=1|test2=2|test3=3 inside it.
#show command works fine and just as i would expect it i.e
#show @test.test1 will show 1, @test.test2=2 @test.test3=3
Now when we start introducing variables into the #show its also quite predictable i.e
#show {@{@name}.test1} where @name=test will be evaluated exactly the same as @test.test1 and show a value of 1 etc as we replace key names
#show {@test.@key} where @key=test2 will be evaluated as @test.test2 and show 2.
Pretty basic stuff and works as intended. However, when i start introducing a #var or #math function to calculate or change the values it gets counter-intutive.
For example:
I use:
#var test.test1 2 and as i would expect it changes the value of the key test1 to 2
#var test.test2 3 will change the value of the test2 key to 3 etc.
When i start to introduce my variable's to switch between the keys inside the test db and change their value.
#var {test.@key} 5 where @key=test2 will change the value of test.test2 to 5 as you would expect it.
Ok, so far so good, but now to the main problem!
Once i try to switch between databases and use a variable @name=test to change them
For example:
#var {@name.test1} 5 where @name=test will not ! set the value of test.test1 to 5 as my logic would dictate.
Could anyone please clarify if there's a special character i need to use to be able to do that, if its possible at all ? |
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Sat Nov 20, 2010 5:15 pm |
I don't get that creative when using db variables names via implicit concatenation, so Zugg will have to say whether that's a bug or not. An easy solution/alternative/workaround is to use the #ADDKEY command. It also has the advantage of being much cleaner, easier to understand, shows exactly what you are trying to do
Code: |
#addkey @name test1 5 |
|
|
_________________ Asati di tempari! |
|
|
|
Anrok Apprentice
Joined: 19 Nov 2010 Posts: 119
|
Posted: Sat Nov 20, 2010 5:59 pm |
Yes, #addkey does indeed work work with this, however as far as i am aware i cant do math using external variables with an #addkey, so something like
#math {%1name.@variable} {%db( @%1, @variable)}+{@lines.%1} cant be replaced with anything #addkey can do. This line of trigger isnt a problem as it works, however
due to the ?inconsistency? if i was to do #math {@dbvariable.%1name} instead of {%1name.@variable} - it wouldnt work! So while i can do maths inside a random key of a certain dbvariable, i cant do math in a random dbvariable inside a certain key. |
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Sat Nov 20, 2010 6:21 pm |
Remember #MATH is just doing a math evaluation. You can either do the math in a local variable and assign it to the DB. You can also just evaluate the expression.
Code: |
#addkey @name test1 ({%db( @%1, @variable)}+{@lines.%1}) |
Something like that should do the trick. |
|
_________________ Asati di tempari! |
|
|
|
Anrok Apprentice
Joined: 19 Nov 2010 Posts: 119
|
Posted: Sat Nov 20, 2010 7:14 pm |
Thanks, you know i just code the way i got used to code, and i never once had to assign a local variable to anything, it's easier for me to just have the numbers retrieved from databases and then do math inside those databases from inside the other database, instead of retrieving everything from the databases assigning things into variables and then putting it into another variable - seems a bit awkward. I knew there was a limit on how far i could go with cross-referencing databases to do math in another database, just didnt think today would be the day.
Hopefully someone will come along and tell me that its possible to use the name for a database from a key in another database and use a key from a third database to pick the key for the first database to do things in and then cross reference and multiply numbers using %db function |
|
|
|
Anrok Apprentice
Joined: 19 Nov 2010 Posts: 119
|
Posted: Sat Nov 20, 2010 8:41 pm |
Hmm, i thought about this some more, came back to your previous post again "Tech", evaluated and integrated it properly and realised that you gave me the exact answer i was looking for without me realizing it first time around.
I never thought #addkey could be used for math using indirect variables and %db, this is a very welcome addition to my cmud knowledge.
Thanks a bunch! |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Mon Nov 22, 2010 1:21 pm |
You can do math in any expression. Simply enclosing a mathematical expression within parentheses will evaluate it mathematically. Technically, #MATH is not necessary any more, but is retained for compatibility reasons.
|
|
|
|
|
|