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


Joined: 03 Mar 2001
Posts: 1127
Location: London

PostPosted: Mon May 14, 2007 1:34 am   

[1.33] novel #switch usage with key/val variable
 
This is purely out of curiosity for now

Say I had a variable, testvar
testvar.one = eat
testvar.two = drink
testvar.three = sleep

#LOOPDB @testvar {~(%key~)~ ~{%val~}}

would output
(one) {eat}
(two) {sleep}
(three) {think}
which would be correct syntax for part of a SWITCH...

so... is it possible to do a #SWITCH that would be able to match all the keys and do the values

#SWITCH %1
%exec(#LOOPDB @testvar {~(%key~)~ ~{%val~}})


To get CMUD to use the variable to generate a #SWITCH based on the contents of a variable, which may grow or shrink as the variable changes etc.

I don't actually have a use for it yet, but it could be nifty ;)
_________________
CMUD Pro, Windows Vista x64
Core2 Q6600, 4GB RAM, GeForce 8800GT
Because you need it for text... ;)
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Mon May 14, 2007 10:41 am   
 
No, you can't dynamically generate code this way, because your command there says "if %1 is true then do this %exec function". You'd have to force the #loopdb to run before the #switch command, perhaps by having the loopdb code in an alias that calls the #switch. There are some pretty simple ways to do the same thing with #loopdb though:

#loopdb @testvar {#if (%1=%key) {#exec {%val}}}

should work, and since two keys can't have the same name it shouldn't send more than one command. Adding an #abort command might also cancel the ifs after matching.
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Mon May 14, 2007 9:12 pm   
 
Urm, Look at the code you just wrote Fang.
#exec {"@testvar."%1}

Tada, I know people don't like to use #Exec but this is certainly faster than a loop.
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Tue May 15, 2007 12:02 am   
 
...You know, concatenation didn't even occur to me. I blame withdrawl symptoms, I've just moved and have no internet at home, so none of my computers with CMUD on are actually useful.

Though it's funny you mention that since someone had a problem with that kind of implicit concat earlier today. It'd probably have to be

#exec {%concat("@testvar.",%1)}
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
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