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


Joined: 19 Nov 2010
Posts: 119

PostPosted: Mon Dec 20, 2010 5:28 pm   

Replacing multiple items in a stringlist
 
Hey again guys, what if i have a number of items in a stringlist with different names and i want to replace it all to the same name, what's the fastest way to do it ?

@stringlist=playerone|playertwo|playerthree|playerfour (etc) (not always ordered) and i want to end up with another stringlist which will have duplicated the word player (without one two three and four etc)

Something like (which doesnt work as replace doesnt accept more than 1 word to replace):
Code:

 #loop %numitems(@stringlist) {#show %additem(%replace(@stringlist,one&two&three&four,""))}
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Mon Dec 20, 2010 7:30 pm   
 
I would loop through the entire list, creating a new list with the modified value as I go. Then if I need to save it in the original variable, I would simply set that variable to the new value. Note that if you do this, you will need to use %additem() rather than #additem, because #additem will remove duplicates as you create it.
Reply with quote
Anrok
Apprentice


Joined: 19 Nov 2010
Posts: 119

PostPosted: Mon Dec 20, 2010 8:57 pm   
 
Hmm, if i have a variable test=playerone, can i make CMUD separate letters from inside that variable and put "player" and "one" inside two different variables ?
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Mon Dec 20, 2010 9:20 pm   
 
Try %match() or %regex()
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Mon Dec 20, 2010 11:56 pm   
 
IF you happen know all of the terms you would like to sift out, you can use a #FORALL loop on those terms and replace them in turn.

Code:
$terms = "blah|blah|blah"
#forall $terms {
  #if (%pos(%i,@stringlist) {
    #call %replace(%ref(stringlist),%i,"")
  }
}
_________________
EDIT: I didn't like my old signature
Reply with quote
Anrok
Apprentice


Joined: 19 Nov 2010
Posts: 119

PostPosted: Tue Dec 21, 2010 12:06 am   
 
Looks pretty smart Matt! Copy pasted and made a @stringlist list with values i wanted to change and it doesnt work. ERROR: Syntax error in Alias: test : unmatched parenthesis.
Reply with quote
Anrok
Apprentice


Joined: 19 Nov 2010
Posts: 119

PostPosted: Tue Dec 21, 2010 12:31 am   
 
On a similar note, could someone please tell me about #loops within a #loop and how do i differentiate between different loop %{i}'s.

For example with this loop here:

@stringlist database:dog=2|cat=3|ferret=1

Code:
#loop %numitems(@stringlist) {#loop @stringlist.%i {tell %{i}.%dbkey(@stringlist,%i) hello}}


What i want these loops to do is tell 1.dog hello, tell 2.dog tello, tell 1.cat hello tell 2.cat hello tell 3.cat hello tell 1.ferret hello. However, the %{i} within the second loop seems to belong to the first loop.

What it does is: tell 1.dog hello twice, telling 2.cat hello thrice and telling 3.ferret hello once.
Reply with quote
Anrok
Apprentice


Joined: 19 Nov 2010
Posts: 119

PostPosted: Tue Dec 21, 2010 12:43 am   
 
found it, its %(j) for the inner loop :)
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Tue Dec 21, 2010 1:44 am   
 
One of the easiest errors to make is insufficient parentheses in an #IF condition. I do it all the time! Here's a corrected version of Matt's script:
Code:
$terms = "blah|blah|blah"
#forall $terms {
  #if (%pos(%i,@stringlist)) {
    #call %replace(%ref(stringlist),%i,"")
  }
}
Reply with quote
Anrok
Apprentice


Joined: 19 Nov 2010
Posts: 119

PostPosted: Tue Dec 21, 2010 2:30 am   
 
Thanks Matt, great script! And thanks Rehab for pointing me towards %match which turned out to be a great function.
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