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
DraxDrax
Apprentice


Joined: 22 Mar 2009
Posts: 149

PostPosted: Mon Jan 11, 2010 5:12 am   

[Resolved] Finding variables that match a certain format, in a particular class
 
The script I am writing at the moment accesses a number of record variables which are all named with the same prefix and contained in the same class.
Is there a simple way to get a list of these variables from within a script? I'd likie to be able to do it without adding extra code that will build and maintain such a list as I add or remove more variables.

To better illustrate what I mean, take this example:

Code:
#CLASS DummyClass
#VAR foo_alpha ""
#VAR foo_beta ""
#VAR foo_gamma ""


From within a script, how can I get a list of all variables in the class DummyClass which have names which match the pattern "^foo_", and return the rest of the name?. In this case "alpha|beta|gamma".


Last edited by DraxDrax on Fri Mar 18, 2011 5:03 pm; edited 1 time in total
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Mon Jan 11, 2010 1:54 pm   
 
No, there really isn't any way to do what you ask. However, if you explain what you are trying to do with this code, we may be able to suggest an alternative. For instance, I suspect that a database variable will work for you. A database variable would have a value that looks like "alpha=1|beta=2|gamma=3". You can retrieve or set the value of alpha, or beta, or gamma in several ways. You can add as many key/value pairs as you need, and even loop through them all.
Reply with quote
gamma_ray
Magician


Joined: 17 Apr 2005
Posts: 496

PostPosted: Mon Jan 11, 2010 3:40 pm   
 
I think part of the problem is that he's already using record variables. In this case he wants a record of record variables, but Zugg has already said this is slow/bad, plus there's a lot of complications with adding/manipulating data. Also, I suspect (just guessing) that he's adding and removing variables by hand, which is why it's such a pain to keep track of all the variables.

But keeping track of them in your script is probably still the only way, unless you want to switch to a full fledged database (which has all the same overhead, except you don't have to write it personally).
Reply with quote
DraxDrax
Apprentice


Joined: 22 Mar 2009
Posts: 149

PostPosted: Mon Jan 11, 2010 5:56 pm   
 
That's precisely what I'm doing, and also the nuisance I was hoping to avoid, gamma_ray.

I'll take this as a definitive no on the question of whether or not there's a simpler way to do it, and set about integrating the appropriate code in to my script to maintain a list of records.

Thank you both for the quick responses!
Reply with quote
DraxDrax
Apprentice


Joined: 22 Mar 2009
Posts: 149

PostPosted: Fri Mar 18, 2011 5:01 pm   
 
It's a bit convoluted, but I've found that this is possible using LUA and the zs object. In case someone else ever cared to try something similar, I thought I'd share my solution.

Code:
<cmud>
  <class name="DummyClass" copy="yes">
    <var name="foo_alpha" copy="yes"/>
    <var name="foo_beta" copy="yes"/>
    <var name="JunkVariable" copy="yes"/>
  </class>
  <var name="foo_gamma" copy="yes"/>
  <var name="OtherJunkVariable" copy="yes"/>
  <alias name="test" copy="yes">
    <value>// assign total number of variables to @lua_var1
#LUA {
  a = zs.getvar("lua_var1")
  a.value = zs.numvar
  }

// loop through every variable
#LOOP @lua_var1 {
  // assign 'name' of variable to @lua_var1
  // assign 'class' of variable to @lua_var2
  #LUA {
    a = zs.getvar("lua_var1")
    b = zs.getvar("lua_var2")
    c = zs.getvar(%i)
    a.value = c.name
    b.value = c.class.name
    }

  // Check name and class of variable
  #IF (%begins( @lua_var1, "foo_") &amp;&amp; @lua_var2 = "DummyClass") {#SAY Fired on @lua_var1}
  }</value>
  </alias>
  <var name="lua_var1" copy="yes"/>
  <var name="lua_var2" copy="yes"/>
</cmud>


The XML snippit creates a DummyClass with three variables in it, the names of two of those variables begin with the string "foo_" while the third does not. An additional two variables are created in the root class, one has a name which begins with the string "foo_" the other does not. The alias 'test' searches for and correctly returns only the two variables in the class DummyClass which have names that begin 'foo_'
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