Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Thu Apr 26, 2007 6:14 pm   

[1.30] Difficulty getting data records to work with local variables
 
I made an alias, similar to an alias I use often in zMUD, for adding values to data record variables more easily. With the recent change to local variables and how they work with #ADDITEM and #ADDKEY, I'm having problems getting the alias to behave the way I want. I know I could just scrap the alias and start all over with a different method of tracking, but I thought this post might help uncover situations where it's difficult or ambiguous as to how local variables work with some commands.

Code:
#ALIAS i_add($var, $keys, $val) {
#IF ($val == "") {
   $val = 1
}
#FORALL {$keys} {
   #ADDITEM ${var}.keys {%i}
}
#ADDKEY {$var} {$keys} {$val}
}


The idea was that I could use this to add one or more keys to a data record variable (whose name is passed in via $var) with an optional value. Typical uses include:

  1. Tracking a single key: i_add flags failsafe
  2. Tracking multiple keys in a single call: i_add afflictions paralysis|stupidity
  3. Tracking special keys with specific values: i_add settings med_bag 24


I've been trying to play with this alias now that the behavior of CMUD has changed, and I can't seem to find a solution that lets me use $var to assign a global variable. Basically, if $var = "afflictions" I want it to set afflictions = (updated data records with new keys and values). I've tried #VAR with various bracket setups. I've tried #EXEC with a variable assignment (it doesn't like the weird separator characters in the data record expansion). Anyone else got ideas?
Reply with quote
Zugg
MASTER


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

PostPosted: Thu Apr 26, 2007 6:32 pm   
 
To use a local variable for indirect variable assignment, the syntax would be: @{$localvar}

The way indirect reference works is that you have @{whatever} and CMUD evaluates "whatever" as a string to produce the name of the variable. So, "whatever" can be any string expression, and using local variables should work. For example:

Code:
#VAR a "hello world"
$local = "a"
#SHOW @{$local}

will display "hello world".

You can *only* access normal variables indirectly. There is no way to access a local variable indirectly. Whenever you use the $ character, it must immediately proceed the variable name. So stuff like ${var} do not work.

Finally, the #ADDKEY (and #ADDITEM, etc) require a variable name as the first argument. So using {$var} probably isn't going to work. In fact, you should never need the syntax like {$var}. Hopefully someone else can help with more specifics on how to make your script work. But you might also look at using the %addkey and %db functions instead of the #ADDKEY command. You don't need to always use the varname.key syntax when you can just use %db("varname","key").
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Apr 26, 2007 6:38 pm   
 
I have an alias that I use with the MXP send command that's this:

#alias vset($name,$val) {#var $name $value;#show $name is now <color %if($value,lime>ON,red>OFF)</color>}

And now, bizarrely, it doesn't set the variable right. If I were to do "vset test 1" the output is "1 is now ON", which is obviously wrong. If I remove the #var line, it works fine. Reckon the two are related?
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Zugg
MASTER


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

PostPosted: Thu Apr 26, 2007 6:41 pm   
 
I actually can't reproduce your issue Fang. Your example gives syntax errors when I try it on the command line.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Apr 26, 2007 6:47 pm   
 
When I typed it out, I typoed. The actual name of the second parameter is $value, not $val.


Last edited by Fang Xianfu on Thu Apr 26, 2007 7:10 pm; edited 1 time in total
Reply with quote
Zugg
MASTER


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

PostPosted: Thu Apr 26, 2007 6:56 pm   
 
Right...even with that change I get errors about the > and < characters. I think you need some quotes somewhere.
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Thu Apr 26, 2007 7:12 pm   
 
My particular issue is with getting values into a non-local variable using the contents of a local variable (parameter with a name) as the variable name. The #ADDKEY command doesn't want the @ on the first parameter and using $var will treat $var as the variable to be assigned to rather than pull the value out and use that as a variable name. For now, I've re-written this alias using %1, %2, and %3 instead, but I'm still making use of local variables for some manipulation of the data. I just need to use %1 to assign the result back into a non-local variable.

I know there isn't really any way to "fix" the parser for a situation like this because it's not really a bug in the parser. It's just the way it has to address things, and I understand that my code needed to change.

Code:
#ALIAS i_add {
$db = @{%1}
$keys = {%2}
$val = {%3}
#IF ($val == "") {
   $val = 1
}
#IF ($db.keys) {
   $db.keys = %dups(%concat($db.keys, "|", $keys))
} {
   $db.keys = %dups($keys)
}
#ADDKEY $db {$keys} {$val}
#VAR {%1} {$db}
}
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Apr 26, 2007 7:14 pm   
 
How weird... it was working a few versions ago. It's compiling fine here, and even with quotes around the "lime>ON" and "red>OFF" parts, it's still producing the error. You could even just have

#alias test($name,$value) {#var $name $value;#show $name}

and the #show command shows whatever you typed as the second parameter. It's like the "#var $name $value" command is actually setting the value of the $name variable.
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Thu Apr 26, 2007 7:32 pm   
 
Have you tried?

#exec {"#ADDITEM "$var".keys {"%i"}"}

I think I used something similar to this before.
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Zugg
MASTER


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

PostPosted: Fri Apr 27, 2007 7:29 pm   
 
Fang, I tried your example and it seems to work fine here. When I type:

test var value

then it properly shows "value" on the screen. This is in a blank default session.

Arminas: that's why I recommended using %additem instead of #ADDITEM. It's always good to try and avoid using #exec when possible, since it can be slow. This should work:

#VAR @{$var} %additem(@{$var}, $key, %i)

Hmm, does this work. Maybe not...I'll have to think about it.

Larkin: You still shouldn't be using syntax like:

$keys = {%2}

why can't you just use

$keys = %2

Maybe you are using a work around for some old bug, but I'd hate to see people using the old sloppy syntax from zMUD.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion 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