Google Lua gmatch
look at this code example https://codea.io/talk/discussion/5605/patterns-tutorial
for Obj1, obj2, obj3 -- Here In Ipairs(str, "%a+, %a+, %a+") -- Here
At the lines where I placed the comment, how many patterns (limit) can you create?
I think you mean global captures from
ipairs is for iterating over a table. I have no idea whether there’s a limit to the number of captures.
I mean, you can have unlimited objects;
for a, b, c, ... z in string:gmatch(stringText, "%a+, %a+, ... , %a+") do
Sorry, for previous post.
I’ve no idea if there’s a limit on the number of matches, but I don’t think you’ve quite got the idea of how
gmatch works. It produces an iterator, like
ipairs does, so generally the idea is to produce patterns that can match certain results and iterate through them, rather than chaining together lots of captures in a single pattern.
For example, here’s one that can capture vector coordinates from an .obj file. It captures an alphanumeric code of varying length
(%a+), and then up to 3 floating point numbers, which could have
. in them, so we have to capture digits and punctuation,
([%d%p]*), and packs them into a vector:
for code, v1,v2,v3 in data:gmatch("(%a+) ([%w%p]+) *([%d%p]*) *([%d%p]*)[\\r\ ]") do if code=="v" then --point position p[#p+1]=vec3(v1,v2,v3)
This is the data it matches against (held in the string
v 0.598267 0.199077 -0.598267 v -0.508672 0.462452 -0.508672 v 0.508672 0.462452 -0.508672 v -0.508672 0.462452 0.508672
So each run through the loop captures one line from the
data string, with between 2 and four captures per line. In this case it makes sense to capture 4 fields at a time, because the four are all part of the same object (a vector).
@TokOut Instead of posting useless questions, why don’t you try for yourself how many patterns you can have. If I said the limit was 1024, would you ever try to use 1024 captures. Chances are you’re not going to use more than a few captures at a time. I don’t think I’ve ever use more than 10 captures at a time with gmatch.
I only wanted to know, because if there would be a limit of 5, then it wouldn’t work.