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
Josiah.Bruns
Apprentice


Joined: 04 Mar 2007
Posts: 103

PostPosted: Thu Mar 15, 2007 8:41 pm   

Whats the best way to turn this Variable into an array list?
 
Code:
#VARIABLE RoomExits { north, south, west, east, northwest and northeast.} {}
#VARIABLE RoomExits { south.} {}


Here are a couple of examples of the roomexits variable.

I basically have to put a "|" in place of comma's and "and", and delete the period at the end of the line, and delete the spaces.

I have looked at the %replace command, and how some success with it, but i am wondering if there is a way to do this very elegantly.

Vijilante wrote some code on a previous post to do this i think, but i couldn't figure out how what he wrote was working.

He wrote:
Code:
#VAR RoomExits {%subchar(%replace(@RoomExits," and ","|")," ,.","||")

I butchured this out of what he really wrote so it may have been other lines of what he wrote that made the above code make sense.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Fri Mar 16, 2007 1:45 pm   
 
Looking at what you have so far a few small adjustments could be made to what I wrote to be a little more efficient, and of course there was a missing closing brace.
#VAR RoomExits {%subchar(%replace(@RoomExits," and ","|"),", .","|")}
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Josiah.Bruns
Apprentice


Joined: 04 Mar 2007
Posts: 103

PostPosted: Fri Mar 16, 2007 10:09 pm   
 
Vijilante can you explain how the %subchar function is interacting with the %replace and what the list of chars and quotes and pipes after the %replace ( ) do?
Reply with quote
Josiah.Bruns
Apprentice


Joined: 04 Mar 2007
Posts: 103

PostPosted: Fri Mar 16, 2007 10:32 pm   
 
by the way vijilante what you wrote works perfectly.... i just don't understand why.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Fri Mar 16, 2007 10:50 pm   
 
These things are wondefully straight forward in a way. Let's start with this example:
#VARIABLE RoomExits { north, south, west, east, northwest and northeast.} {}
#VAR RoomExits {%subchar(%replace(@RoomExits," and ","|"),", .","|")}
This effectively results in (because @RoomExits is the innermost item requiring expansion or evaluation):
#VAR RoomExits {%subchar(%replace(" north, south, west, east, northwest and northeast."," and ","|"),", .","|")}
The replace function then being the innermost item requiring expansion its job gets done:
#VAR RoomExits {%subchar(" north, south, west, east, northwest|northeast.",", .","|")}
Now we finally get to the subchar. What happens here is that characters in the first replacement list are matched then all replacements are made with the characters in the second based on position. Since the first list contains more characters then the second all those that don't have a match in the second are simply removed. You can see the results for yourself.

Most likely you were just a little confused by the lack of spaces between parameters. I tend to not bother with unnecessary spaces as I feel that every scrap of screen space is valuable.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Josiah.Bruns
Apprentice


Joined: 04 Mar 2007
Posts: 103

PostPosted: Sat Mar 17, 2007 3:18 am   
 
I must not have taken my smart pills today cuz i still a bit confused. let me see if i can explain where.

According to the help files
Quote:

Syntax: %replace(s,p,r)
return s with all occurrences of p replaced with r


So if:
#Var RoomExits= { north, south and east.}
%replace(@roomexits, " and " , "|" ) just replaces the " and " with the pipe. ... ok i see that part now. The spaces were messing me up.

Now for the next part:

Quote:

Syntax: %subchar(s,list1,list2)
Replaces characters of string s contained in list1 with the corresponding character in list2


%subchar(" north, south|east. " , " , . " , "|" )

So the comma's are replaced with the pipe and the second char in list1 which is a period has not matching char in list2 so its just removed.

? how come the spaces go away? and is there any function like subchar that would work with an list instead of a char list, so you could just have one function that looked kinda like this:

%sublist(@RoomExits, "and|,|." , ""|" | "|"")
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Sat Mar 17, 2007 3:26 am   
 
There's not one single function that'll do that for you, no.

The reason the spaces are removed from your string when you use %subchar is because the list2 parameter contains a space. list1 in that example actually contains three elements - a comma, a space and a full stop/period. list2 only contains one item, a pipe, so the commas are replaced with pipes and the spaces and full stops/periods are replaced with nothing.
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