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
kjaerhus
Magician


Joined: 18 Dec 2006
Posts: 317
Location: Denmark

PostPosted: 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
-----------------
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: 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
Reply with quote
kjaerhus
Magician


Joined: 18 Dec 2006
Posts: 317
Location: Denmark

PostPosted: 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?
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: 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.
Reply with quote
Zugg
MASTER


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

PostPosted: 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.
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Thu Apr 01, 2010 2:03 am   
 
Hmm, I could see uses for a reference to an object :)
_________________
Sic itur ad astra.
Reply with quote
kjaerhus
Magician


Joined: 18 Dec 2006
Posts: 317
Location: Denmark

PostPosted: 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.
Reply with quote
kjaerhus
Magician


Joined: 18 Dec 2006
Posts: 317
Location: Denmark

PostPosted: 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.
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