|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: 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. |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: 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}. |
|
|
|
Larkin Wizard
Joined: 25 Mar 2003 Posts: 1113 Location: USA
|
Posted: 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.
|
|
|
|
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: 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 |
|
|
|
Larkin Wizard
Joined: 25 Mar 2003 Posts: 1113 Location: USA
|
Posted: 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.
|
|
|
|
|
|
|
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
|
|