|
Progonoi Magician
Joined: 28 Jan 2007 Posts: 430
|
Posted: Tue Aug 26, 2008 8:07 pm
Subregex, I think I'll give up. |
I know I'll be said that if you don't understand it just don't use it most likely, but I'd really like to think straight for me all the things this, most likely, most powerful function contains.
However, after reading both usual and advanced help dozens of times, I'm still just as dumb as I was when I figured I'll take it up.
I'm willing to bet that the code I'm going to paste below is possible to be done with one single %subregex code line but I'm just not seeing the light. Maybe I don't deserve to
Code: |
<pattern>\[Exits\: (.*)\]$</pattern>
<value>#local $return $rawExits $rawExitsCount
$rawExits=%sort(%subchar(%subregex(%1,"(.*)",\1)," ","|"))
$rawExitsCount=%numitems($rawExits)
#loop $rawExitsCount {#additem $return %left(%item($rawExits,%i),1)}
#show $return
</value>
|
And before someone'd just post a more or less complex outcome, let me tell you that I actually want to know what each bit of my general code does. So unless someone is willing to play interactive help file, there's no need to bother.
Rant aside, here's what the code is supposed to do:
1. captures directions data from Exits line.
2. originally it will be in form of
Code: |
north south east west
|
3. then it will remove the spaces and replace them with pipes
4. then it will sort the outcome.
5. then it will make a note how many items are in that string list.
6. then it will loop it that number of times to get the outcome list.
7. the eventual outcome list should be in the form of n|s|e|w.
I really hope I'm not coming across smug, but to be honest once I understand the basics about something but subconsiously feel that there are so much more about what I just can't find enough reasonable help (as I'm not a programmer by trade), it really pisses me off. |
|
_________________ The Proud new owner of CMud.
--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
-------------------------------- |
|
|
|
Toxic Adept
Joined: 27 May 2008 Posts: 299
|
Posted: Tue Aug 26, 2008 8:26 pm |
Just do
Code: |
#LOCAL $return
#FORALL %subchar(%1," ","|") {#ADDITEM $return %left(%i,1)}
#SHOW $return |
personally, I think you're trying to make it too complex. |
|
|
|
Vijilante SubAdmin
Joined: 18 Nov 2001 Posts: 5182
|
Posted: Tue Aug 26, 2008 10:57 pm |
Basically if it has to do with a string manipulation then subregex can do it.
Code: |
<pattern>\[Exits\: (.*)\]$</pattern>
<value>$return=%subregex(%1,"(?:(\w)\w+)|( )","(?(2)\||\1)")
#show $return
</value> |
First what the regex matches. We have 2 different things that can be matched "(\w)\w+" and "( )". The reason for the structure with the first possibility is so that it captures the first character. The second is quite obvious we are matching a space, and capturing it.
Second is the substitution. Here we check if the space was captured with the conditional syntax. We have to use "\|" to indicate the pipe character within the conditional, because it is a special character at that time. When we didn't capture the second capture the first must have been matched so we use that single character that was captured as the substitution. The match in that case covered the entire word which means the whole word gets replaced by its first character. This same substitution could be written with a slightly simpler condition, but I swapped the logic to provide a greater set of examples. The simpler one would be "(?(1)\1||)"
Put the use of %sort where you actually need it. |
|
_________________ The only good questions are the ones we have never answered before.
Search the Forums |
|
|
|
Progonoi Magician
Joined: 28 Jan 2007 Posts: 430
|
Posted: Wed Aug 27, 2008 12:29 pm |
Whoa! Yeah, this works exactly like I wanted it to. Vij, you're the great!
But bear with me here because I want to completely understand.
Pattern structure: First part is supposed to capture first character of a word, right? And as the words in pattern are followed by spaces, you have to capture space as well? But why do it with
..? Using
wouldn't work?
To tell you the truth I didn't know that you can capture the first character like this but now that you showed it, it seems pretty obvious I suppose.
Substitution part: I've read your explanation from top to bottom several times but its still not bouncing back to me. I understand that it consists of two parts. First is
where there's a space after pipe.
Whats the significance of "?" and "(2)" here?
Second part is
The thing is, I feel like I may be understanding it but still can't completely wrap my head around your explanation.
Hrm. Ok, maybe I now got it...
? marks condition clause just like in usual regex. (2) marks that it tries to capture second part of the regex's condition. When it doesn't match it, it means it matches the first one with is first character and its captures into \1?
Well, maybe my cup of morning coffee helps, but I have a feeling that not completely |
|
_________________ The Proud new owner of CMud.
--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
-------------------------------- |
|
|
|
|
|
|
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
|
|