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
Thrillhouse8
Beginner


Joined: 21 Sep 2004
Posts: 18

PostPosted: Tue Sep 09, 2008 9:22 am   

Looping within a command
 
Consider a variable @varname with a variable number of items(including zero), which are names of record variables themselves:
Code:
#VAR varname {db1|db2|db3|...|dbn}

where @db1.name = Name1.

Now, there's also a user-defined function @capitalize which can manipulate any of the individual items in @varname, for instance, returning @dbX.name in all caps. So, by typing #SHOW @capitalize(@db1.name), output would read NAME1.

I'm looking for a clean way to do this inside an alias, which would display the results in one sentence.

    Input:
    aliasname

    Output:
    Blah, blah, blah, NAME1, NAME2, NAME3,..., and NAMEN. Blah, blah blah.


Any thoughts or suggestions?
Reply with quote
Thrillhouse8
Beginner


Joined: 21 Sep 2004
Posts: 18

PostPosted: Tue Sep 09, 2008 9:31 am   so far
 
*EDIT*
What I'm currently leaning towards is using another alias

Code:
#var stringname ""
#FORALL @varname {
  #IF (%eval( %ismember( %i, @varname)) < %eval( %numitems( @varname))) {
    #VAR stringname %concat( @stringname, @capitalize(@{%i.name}))
    #VAR stringname %concat( @stringname, ", ")
    } {
    #VAR stringname %concat( @stringname, "and ")
    #VAR stringname %concat( @stringname, @capitalize(@{%i.name}))
    }
  }


and just calling @stringname in the original alias. It's working all right, just wondered if there's a nicer way.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Tue Sep 09, 2008 10:04 am   
 
How about letting %replace do the looping.
Code:
#var stringname {%upper(%expand(%concat("@{",%replace(@varname,"|",".name}, "),"}.")))}

Then to handle the "and " you wanted you can do this
Code:
#VAR stringname {%insert("and ",@stringname,%regex(@stringname,".*?, ."))}
Basically the execution speed should be about 10 times faster then what you have now.
_________________
The only good questions are the ones we have never answered before.
Search the Forums

Last edited by Vijilante on Tue Sep 09, 2008 10:15 am; edited 1 time in total
Reply with quote
Thrillhouse8
Beginner


Joined: 21 Sep 2004
Posts: 18

PostPosted: Tue Sep 09, 2008 10:07 am   
 
The purpose of my function isn't actually to capitalize; I just threw that in there as an example. There is manipulation of the data.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Tue Sep 09, 2008 10:19 am   
 
Ok, the same principle applies.
Code:
#var stringname {%expand(%concat("@capitalize(@{",%replace(@varname,"|",".name}), @capitalize(@{"),"})."))}
It is just as well, I was missing some stuff in my first post. I really should know better then to post on my first cup of coffee.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
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