Jump to content




gmatch, multiple captures, infinite for loop


  • You cannot reply to this topic
3 replies to this topic

#1 valithor

  • Members
  • 1,053 posts

Posted 25 May 2015 - 08:40 PM

I am attempting to sift through a string and get a number of different things out of it, but the loop that it is running in does not seem to ever end. As of posting this, I am running a test to see how many times It runs, and it has run over 150,000 times, and it is still going. This leads me to believe I have done something wrong causing it to never end.

code snippet:
siteContents = "Anavrins [BG:BLACK]test site! [C:RED][CENTER]NOTHING IS REAL[/CENTER][CENTER][C:LIME]i'm editing this site in notepad++[/CENTER][CENTER]i made a markup language. :)/>/>/>/>[/CENTER] [BR][C:WHITE]make it rain krist wooo [BR][BR]by the way nice hat[BR][CENTER]centered text [C:RED]with color[C:WHITE] took [HL:RED]forever[HL:BLACK] to make[/CENTER][CENTER]also, the refresh button barely works[/CENTER] i'm just writing random stuff now.[BR]comp id: [ID][BR]PHP random number: 759704590[BR][C:THEMEFG]fuck shit gets its color from the theme[CR]this text[END]"

counter=0
for pword,tag,arg,aword,space in siteContents:gmatch("(%w*)%[?(%/?%w*):?(%w*)%]?(%w*)(%s*)") do -- preword, tag, argument, afterword, space
  counter=counter+1
  print(counter)
  sleep()
end

What this does is convert a string like this:
"Hello[COLOR:RED]There " into 5 variables
"Hello","COLOR","RED","There"," "

pastebin for the code: http://pastebin.com/VV7mJ5Ym

My question is what is causing it to never end. Is it the multiple captures, a horrible pattern, or something else?

Edited by valithor, 25 May 2015 - 09:09 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 25 May 2015 - 09:22 PM

I'm going to guess the problem is the number of optional arguments in your pattern.

It's capturing a lot of stuff weirdly - try printing out some of your variables if you don't know what I mean.

#3 valithor

  • Members
  • 1,053 posts

Posted 25 May 2015 - 09:30 PM

Just got a interesting result...

In mimic the pattern acts as expected, and stops after 87 iterations maybe it is CC?

I might be forced to split the one long pattern up into multiple ones.

edit:
When the server I test on stops lagging I will try and print out the variable using CC to see what happens after the 87th iteration.

Edited by valithor, 25 May 2015 - 09:32 PM.


#4 valithor

  • Members
  • 1,053 posts

Posted 25 May 2015 - 11:00 PM

I found a work around by using the tokenise function from the shell api and just passing each individual word to the pattern.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users