lua-users home
lua-l archive

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



* On 2008-06-03 Andrew Wilson <agrwagrw@gmail.com> wrote  :

> Lua list:
> 
> While trying to do a nice natural sorting style example for Lua based
> on site http://www.davekoelle.com/alphanum.html site, I wasn't verify
> satisfied with my example,
> does anyone have a better method of splitting a string into table of
> strings and numbers, than my chunkString function below. I know I'm
> missing some simple string.gmatch way of doing it. Whole example is
> included...

What about:

function chunkString(str)
        local c = {}
        for a, b in str:gmatch("(%d*)(%D*)") do
                if #a>0 then c[#c+1] = tonumber(a) end
                if #b>0 then c[#c+1] = b end
        end
        return c
end

-- 
:wq
^X^Cy^K^X^C^C^C^C