 |
kjaerhus Magician

Joined: 18 Dec 2006 Posts: 317 Location: Denmark
|
Posted: Wed Mar 31, 2010 11:19 pm
[3.16b] Assigning a record variable to a local variable |
I found some strange behavior here. Check out the following alias:
Code: |
#VAR someVar ""
#ADDKEY someVar {Santa=Claus}
$localVar = @someVar
#VAR globalVar @someVar
#ADDKEY someVar {Easter=Bunny}
#SAY "-----------------"
#SAY @globalVar
#SAY "-----------------"
#SAY $localVar
#SAY "-----------------"
#SAY @someVar
#SAY "-----------------"
#UNVAR someVar
#UNVAR globalVar |
...and it's result:
Code: |
-----------------
Santa=Claus
-----------------
Santa=Claus|Easter=Bunny
-----------------
Santa=Claus|Easter=Bunny
----------------- |
It seems when you assign a record variable to a local variable you get the reference instead of the value which is a bit confusing. I would expect it to get the value and behave like the normal variable. Apparently it works fine with strings:
Code: |
#VAR someVar2 "Santa Claus"
$localVar2 = @someVar2
#VAR globalVar2 @someVar2
someVar2 = @someVar2" - Easter Bunny"
#SAY "-----------------"
#SAY @globalVar2
#SAY "-----------------"
#SAY $localVar2
#SAY "-----------------"
#SAY @someVar2
#SAY "-----------------"
#UNVAR someVar2
#UNVAR globalVar2 |
...produces:
Code: |
-----------------
Santa Claus
-----------------
Santa Claus
-----------------
Santa Claus - Easter Bunny
----------------- |
|
|
|
 |
charneus Wizard

Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Wed Mar 31, 2010 11:43 pm |
When wanting to display database variables, you use #SHOWDB and not #SAY
You can also do:
#LOOPDB somedatabase {#SAY {%rec %val}}
However, I'm not really sure what you're aiming for, as strings and databases are not the same thing at all.
Charneus |
|
|
 |
kjaerhus Magician

Joined: 18 Dec 2006 Posts: 317 Location: Denmark
|
Posted: Thu Apr 01, 2010 12:05 am |
I would expect a record variable to behave the same way as as string. After all it's just a string formatted in a certain manner.
Besides it's mostly the difference between a local variable and a normal variable that is interesting. Notice the value of the local variable in the first example. Is it as you would expect it to be? |
|
|
 |
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: Thu Apr 01, 2010 1:05 am |
I never use the #VAR syntax, however, I don't have any problems with assignments. In fact, I read actual database records into variables all the time and they look right.
This works fine, for instance:
Code: |
$rec = @comsql.Execute($sql,@noparam)
|
$rec will contain a database record if it is a single row or a string list or records if there are multiple rows. If I, instead, assign them so a permanent variable:
Code: |
rec = @comsql.Execute($sql,@noparam)
|
the variable will also be formatted correctly.
You can use #ECHO or #SAY to display a database record but only because all byte sequences are technically printable. As Charneus said, the proper way to display an database record in CMUD is with #SHOWDB. |
|
_________________ Sic itur ad astra. |
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Apr 01, 2010 1:24 am |
Yes, I can see this problem. The issue is that a database record has a hash table behind it. So it's not a simple string value any more, it points to an object. And obviously that object is getting passed to the local variable instead of being copied. I'll add that to the bug list.
|
|
|
 |
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: Thu Apr 01, 2010 2:03 am |
Hmm, I could see uses for a reference to an object :)
|
|
_________________ Sic itur ad astra. |
|
|
 |
kjaerhus Magician

Joined: 18 Dec 2006 Posts: 317 Location: Denmark
|
Posted: Thu Apr 01, 2010 8:20 am |
Zugg wrote: |
Yes, I can see this problem. The issue is that a database record has a hash table behind it. So it's not a simple string value any more, it points to an object. And obviously that object is getting passed to the local variable instead of being copied. I'll add that to the bug list. |
Thanks. |
|
|
 |
kjaerhus Magician

Joined: 18 Dec 2006 Posts: 317 Location: Denmark
|
Posted: Thu Apr 01, 2010 8:29 am |
Anaristos wrote: |
Hmm, I could see uses for a reference to an object :) |
As in pass by reference? Well, I suppose it could be useful, but not really when assigning variables. |
|
|
 |
|
|