Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Iterating over a string... in a wacky way.

Iterating over a string... in a wacky way.

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Worstje   Netherlands  (899 posts)  Bio
Date Tue 22 Apr 2008 05:33 AM (UTC)
Message
Hello all,

Let's get to the point right away. =) Right now I am looping through a string with `for word in str:gmatch("%w+") do`.

Is there any way in which I can write a subloop that will take `word` and all that remains, and then continues to take words away?

For example, I have the string 'a b c'. Now I process 'a' in my outer-loop, and inner-loop should go ['a b c', 'a b', 'a']. Next 'b' goes ['b c', 'b']. Last comes 'c', which goes ['c'].

I can personally only think of an ugly way to do this with two while loops, but I have the feeling it could be done in a neater fashion. Is there anyone here willing to point me in the right direction?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Tue 22 Apr 2008 07:11 AM (UTC)
Message
Is this an accurate restatement:


Let w be the first word of the string s.
Let s2 be a copy of s.
While there are words in s2:
  Remove the last word from s2.
Remove w from s.
If s is empty, end; else, go to beginning.


I ask because your outer loop seems to go forward whereas your inner loop goes backward. Your English explanation didn't quite correspond to that, so I wanted to make sure I knew exactly what you're trying to do.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Tue 22 Apr 2008 07:26 AM (UTC)
Message
Try this thread:

http://www.gammon.com.au/forum/bbshowpost.php?id=6544

Amongst other things there is a "general splitting iterator" which is pretty much what you are trying to do. Rather than split at newlines, you want to split at spaces. However if you really need the "first word" and "rest of line" you may need to modify it a bit.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #3 on Tue 22 Apr 2008 09:48 PM (UTC)
Message
Heh, it seems I have bothered all of you for nothing. You'll always see you think about a problem for a few hours and can't figure it out, then you ask for help and the next time you think of the issue, you solve it almost naturally.


local words = {}
local skipTillWord = 0
for word in description:gmatch("%w+") do words[#words+1] = word end
for i=1,#words do
	if (i > skipTillWord) then
		
		for j=#words,i,-1 do
			local substring = table.concat(words, " ", i, j)
			
			if (WordCombinations[substring] ~= nil) then
				-- Bingo. Process it in whatever way I want to.
				
				-- Make sure not to recognise sub-word-combinations part of this one.
				skipTillWord = j
				break
			end
		end
	end
end


It is indeed so that the outer loop counts up, whereas the inner loop counts down. I need to match the biggest possible word combinations and not match the smaller versions if it can be helped. (Suppose WordCombinations has both keys "a b", "a" and "b", it should track the "a b" where possible and not the lonely "a" nor lonely "b"). There's still a minor case of overlapping that isn't handled (inputting "a b c" while WordCombinations contains "a b" and "b c" will only result the "a b" match and never the secondary one), but for my code that isn't of any importance.

I hope this makes clear what I was trying to achieve. =)
Top

Posted by Jing Wu   Australia  (35 posts)  Bio
Date Reply #4 on Wed 25 Jun 2008 08:47 AM (UTC)
Message
follow me guy.

the way is to dynamicly generate the reg expression.

eg:
word combination: "a b c", you can generate some reg expresses in a particular order you wanna do the checking.
then put them in a table like {"a b", "b c"}. finally do a loop to reg.new(tables[i]):exec("a b c").

thatz all.


Kernel Development
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


17,105 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.