[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua way to do this code
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Wed, 17 Oct 2012 21:37:57 -0300
> Firstly, i hope that this is proper place for my small question
Sure is. Everyone is welcome.
> So i kindly ask: how would you do this iteration?
>
> local ret = {}
> for p in string.explode(str, ";") do
> local k,v = string.gmatch(p, "([xyz])=(%d+)")()
> ret[k] = tonumber(v)
> end
string.explode is not a standard Lua function but I can image what it'd do.
string.gmatch is not what you want here: you want string.match.
string.gmatch is for loops, like the one below which is what I'd write
for this task:
for k,v in string.gmatch(str,"([xyz])=(%d+)") do
ret[k] = tonumber(v)
end
The point is that there is no need to split str before doing the match.