lua-users home
lua-l archive

Re: Lua way to do this code

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> 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.

AltStyle によって変換されたページ (->オリジナル) /