List Variables

This code strips all --[[ comments from the input text, including things like --[===[ and such.

            local pat = "%-%-%[(=*)%[.-%]%1%]"
            local ans = text:gsub(pat,"")

@RonJeffries I’ve been trying to use patterns to replace all my if statements, but I’m having trouble with some of the combinations.

The things I’m having trouble with are

d=qwe…asd
The qwe…asd should split to
d
qwe
asd

e=123…456…qwe
Should split to
e
123
456
qwe

f=s:get.func_qwert
Should split to
f
s:get.func_qwert

function setup()
    str="111 table.insert _q34=123.45 _73ab='asd' f:search b=51*52 c=qwert_asdfg d=qwe..asd e=123..456..qwe f=s:get.func_qwert"
    print(str)
    
    for a in string.gmatch(str,"[%d%.%d]*[%_%w]*[%a%.%a]*[%a%:%a]*") do
        if a~="" then
            print(a)
        end
    end
end

you have pattern elements like [%d%.%d ]*

what are you trying to match with those?

i’m not sure you can go all that in one match, but i’m not clear what you want yet. reading further…

@RonJeffries The %d%.%d captures numbers that have a decimal point in them, ie 123.45 . All of those patterns work, I just can’t get the right patterns to capture everything the way I want. If you run the little program I included, it separates everything, but not quite the way I want for everything.

My if statements in the original program does everything the way I want. I just thought I’d try using patterns. It gives me practice trying to use them.

Here’s something that may be a bit like what you are looking for.

note there is no pattern that will parse arbitrary lua code, so we will always have to segment somehow.

function setup()
    str="111 table.insert _q34=123.45 _73ab='asd' f:search b=51*52 c=qwert_asdfg d=qwe..asd e=123..456..qwe f=s:get.func_qwert "
    print(str)
    --pat = "[%d%.%d]*[%_%w]*[%a%.%a]*[%a%:%a]*"
    pat = "([^%s]-)%s" -- split on space
    for a in string.gmatch(str,pat) do
        if a~="" and a:find("=") then
            pat2 = "(.*)=(.*)" -- split and capture around =
            for b,c in a:gmatch(pat2) do
                print(b, " gets ",c)
            end
        else
            print(a, " has no =")
        end
    end
end

yes, i’m not saying patterns are better … just that i’d use them if i could. I’m sure one pattern can’t do what your original code does.

@RonJeffries That’s kind of interesting. I ran it against an actual project and it didn’t turn out to be that beneficial. Majority was has no = .

One pattern won’t do what I’m after, that’s why I have several different [patterns] in the “ “. But the more I add, the more they conflict with the other ones. At least I’m getting practice using them.

yes. the scheme i’m using at home is multiple specialized patterns, run sequentially. one finds class devs, another finds method devs, etc.

@RonJeffries Using patterns is an simple and powerful way to find things. The only problem is they’re not that easy to learn. For me it’s more of trial and error before I get what I want.

me too, though i’m getting better at guessing. often i build up the pattern bit by bit.