This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
valithor's profile picture

gmatch, multiple captures, infinite for loop

Started by valithor, 25 May 2015 - 07:40 PM
valithor #1
Posted 25 May 2015 - 09: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 on 25 May 2015 - 08:09 PM
KingofGamesYami #2
Posted 25 May 2015 - 10: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.
valithor #3
Posted 25 May 2015 - 10: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 on 25 May 2015 - 08:32 PM
valithor #4
Posted 26 May 2015 - 12:00 AM
I found a work around by using the tokenise function from the shell api and just passing each individual word to the pattern.