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


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Thu May 15, 2008 1:40 pm   

Fun with stringlist unions
 
I have discovered an interesting trick that I thought I would share. You can create a varfunc which is the union of two or more stringlists.
Code:

#var alpha {a|b|c}
#var beta {x|y|z}
#varfunc alphabeta {%concat(@alpha,"|",@beta)}
#show @alphabeta
#show y is at %ismember(y,@alphabeta)

This is actually pretty obvious when you think about it, but I found it to be a really cool way to work with sets of lists that I sometimes want to work with separately, and sometimes together. Such as: @armor, @clothing, @weapons, and @allitems. You can't modify the items in the union directly, of course, but it works in triggers and other times when you don't want to modify the list.
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Thu May 15, 2008 1:58 pm   
 
And, of course, you can't sort it or anything like that. But it has its uses, I agree. Nice one.

In a trigger, btw, you should be able to just do {@var1|@var2}.
_________________
Rorso's syntax colouriser.

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


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Thu May 15, 2008 4:22 pm   
 
You may run into problems with empty string lists, and you might want to account for duplicates, too, depending on your usage of the resulting string list.
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Thu May 15, 2008 11:42 pm   
 
When faced with similar situations, I take the extra step to convert both stringlists to string, then concatenating them with the separator I used to convert them to string, then replacing said separator with the stringlist separator. This avoids the problems mentioned by Larkin concerning empty lists.
So:
Code:

alpha     = "a|b|c"
beta      = "x|y|z"
alpha     = %expandlist(@alpha," ")
beta      = %expandlist(@beta," ")
alphabeta = %sort(%replace(%concat(@alpha," ",@beta)," ","|"))

Or:
Code:

alpha     = "a|b|c"
beta      = "x|y|z"
alpha     = %replace(@alpha,"|"," ")
beta      = %replace(@beta,"|"," ")
alphabeta = %sort(%replace(%concat(%trim(@alpha)," ",%trim(@beta))," ","|"))


The separator I use here is a blank, but one must choose one that doesn't collide with the data.

EDIT: Thanks, Larkin. Code fixed!
_________________
Sic itur ad astra.

Last edited by Anaristos on Fri May 16, 2008 10:25 pm; edited 1 time in total
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Fri May 16, 2008 1:50 pm   
 
Well, if you used a separator other than a blank space, you could do the %trim trick. (Missed a % on the one replace function call, too.) But, yes, I was suggesting extra checks and/or function calls might be in order.
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