lua-users home
lua-l archive

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

2015-11-07 17:52 GMT+02:00 Marco Atzori <>:
> Il 07/11/2015 16:46, Matthew Wild ha scritto:
>> If you have actual constraints, revealing them might allow people to
>> help you better. If you don't have any constraints and just want it to
>> perform well, then there are some really much simpler and more
>> efficient solutions than your implementation.
> This code is part of a videogames addon, and is executed every frame refresh
> (about every tenth of a second) for which using tables created in
> continuation is a considerable waste of memory. If there are simpler
> solutions, show them to me as well. I asked your help for this reason ;-)

[from original post]

I cannot envisage why that argument '46' should be needed at all.

I would use a table created once and for all for each string.

local tok_meta = { __index = { gettok =
     local n=#tbl
     j = j or i
     if i and i<0 then i=n+i+1 end
     if j and j<0 then j=n+j+1 end
   return table.concat(tbl,tbl.sep,i,j) end }}

local tokens = function(str,sep)
   local fields={sep=sep}
   return setmetatable(fields,tok_meta)

local s = tokens( "",".")

> s:gettok(1)
> s:gettok(-2)
> s:gettok(2,4)
> s:gettok(-3,-1)