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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
sda
Wanderer


Joined: 18 Apr 2001
Posts: 55
Location: United Kingdom

PostPosted: Tue Aug 13, 2002 7:10 pm   

Multiple #IF and %case
 
Hello

Is there a way of shortening this using %case or another function? Correct me if I'm wrong, but I thought multiple #IF statements like this were 'suboptimal'.

Or does it matter how I write it as long as I can understand it because the interpreter has to basically convert it to #IF regardless of whether I use %case or not?

#IF %pos( "(w)", @strx) {#VARIABLE ttemp string1}
#IF %pos( "(g)", @strx) {#VARIABLE ttemp string2}
#IF %pos( "(b)", @strx) {#VARIABLE ttemp string3}
#IF %pos( "(d)", @strx) {#VARIABLE ttemp string4}

Thanks,
Steve :)
Reply with quote
Kjata
GURU


Joined: 10 Oct 2000
Posts: 4379
Location: USA

PostPosted: Tue Aug 13, 2002 7:25 pm   
 
Using %case would actually end up in more lines of zScript. However, you can make it a bit faster by nesting the if's. It appears that the variable may either contain "(w)", "(g)", "(b)" or "(d)" but not more than one of these. If you nest the if's, then once one of them matches, the other won't be checked. You should also put them in the order they are most likely to be matched. So, suppose the most common occurance is that it contains "(b)", the next common one is "(g)" and then "(w)" and "(d)". In this case, you do:
#VAR ttemp %if(%pos("(b)", @strx), "string1", %if(%pos("(g)", @strx), "string2", %if(%pos("(w)", @strx), "string3", %if(%pos("(d)", @strx), "string4", ""))))

But, all in all, it really shouldn't make a big difference if you turn it into a case statement or a bunch of if's. In reality, a case is just a specialized form of nested if statements. At the end, when translated into machine code, both still need to jump around in the code, which means that the processor stack must be cleared each time, which is what really causes the slowdown, but it is still not noticeable.

Kjata
Reply with quote
sda
Wanderer


Joined: 18 Apr 2001
Posts: 55
Location: United Kingdom

PostPosted: Tue Aug 13, 2002 8:49 pm   
 
Thankyou Kjata, I didn't realise %if existed - lots of possibilities here. It works a treat, but I have just one further question.

#VAR ttemp %if(%pos("(b)", @strx), "string1", %if(%pos("(g)", @strx), etc...

"string1" is actually the name of a variable

I then do #ADDITEM @{@string1} mytext, which works, but also adds a @ at the beginning of the variable every time it makes a pass. After 5 passes I get something like:

@@@@@|The|quick|brown|fox|jumps

#SH @{@string1} works jsut fine, but #ADDITEM doesn't seem to be able to deal with it.

Many thanks :)
Reply with quote
sda
Wanderer


Joined: 18 Apr 2001
Posts: 55
Location: United Kingdom

PostPosted: Tue Aug 13, 2002 9:17 pm   
 
Sorry, I solved it - I took a nap and "Eureka!"

If the name of the variable was stored in another variable called TEST, then I only needed to do #ADDITEM @TEST blah

I feel silly
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD 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