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
ReedN
Wizard


Joined: 04 Jan 2006
Posts: 1279
Location: Portland, Oregon

PostPosted: Sun Mar 23, 2008 4:03 pm   

A question about functions
 
I had created a function that I can either pass a single item or a list of items. Thus I thought to use it like this:

function1( one)

or

function1( one|two)

However, if I call it with a list as it is above, it will only pass the first item in the list. In the above example with one|two, it will only pass 'one' to the function. It takes parenthesis around it as in function1( "one|two") for it to pass both.

Could someone explain why it needs parenthesis to pass the list properly?
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Sun Mar 23, 2008 4:32 pm   
 
I'd assume because CMUD's performing some evaluation on it - | used to be the or operator, so perhaps it's still doing an or on the contents, which should return the first item in the list (in that example, at least). It should work fine with stringlists stored in variables; adding quotes to the ones you're passing manually to explicitly state that they're stringlists shouldn't be much hardship, since you only have to write the code once.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Sun Mar 23, 2008 9:27 pm   
 
I find it interesting that you are having this problem because I tried the following:
Code:

#FUNCTION temp {#RETURN %1}

Then:
Code:

#ALIAS test {#ECHO @temp(one|two)}


The result of entering the alias test on the command line was that one|two was echoed on the screen..
There shouldn't be any reason why CMUD would evaluate a literal. The only time one would expect evaluation is when one passes it a variable as a parameter.
Perhaps you should show us the body of the function. I suspect that problem lies there.
_________________
Sic itur ad astra.
Reply with quote
ReedN
Wizard


Joined: 04 Jan 2006
Posts: 1279
Location: Portland, Oregon

PostPosted: Sun Mar 23, 2008 11:41 pm   
 
I find it really interesting as well because I tried creating another simple function and it passed it just fine, so I went back to my original case and tried it again and it still isn't passing both. Maybe you can see what I'm doing wrong:

<func name="flag" id="3983">
<value>#say -- %1
#if (%numitems( %1) &gt; 1) {
$present = 0
#forall %1 {#if (%iskey( @flags, %i)) {$present = 1}}
#return $present
} {#return %if( %iskey( @flags, %1), %db( @flags, %1), 0)}</value>
</func>

<alias name="tester" id="4212">
<value>#addkey flags writhing 1
#say - @flags
#if (!@flag( writhe_try|writhing)) {
#say here
}
#var flags %null</value>
</alias>

When I run the tester alias it adds writhing to the db variable flags and then calls the flag function to see if it is present. When it is called it only sees the 'writhe_try' and not both items. Weird!?!
Reply with quote
ReedN
Wizard


Joined: 04 Jan 2006
Posts: 1279
Location: Portland, Oregon

PostPosted: Sun Mar 23, 2008 11:50 pm   
 
This piece of information may be key, I just found that if I call the function outside of an if statement it will pass both arguments. If it is inside an if statement it will only pass one. Is this a bug?
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Mon Mar 24, 2008 1:17 am   
 
Almost certainly a bug - like I said, it's performing or evaluation. Or returns the first item if it's true and the second item if it's not. You're getting the first because it's the result of an or operation. But the or operator was changed from | to || in CMUD, presumably to stop exactly this kind of thing from happening. So something's wrong that's making CMUD still use | for or.

It should work fine if the stringlist is in a variable, and if it's not, you can just put quotes round it, anyway.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
ReedN
Wizard


Joined: 04 Jan 2006
Posts: 1279
Location: Portland, Oregon

PostPosted: Mon Mar 24, 2008 2:31 am   
 
Thanks, I'll add a note on the beta forum noting the bug.
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