lua-users home
lua-l archive

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

Il 07/11/2015 18:54, Dirk Laurie ha scritto:
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( "",".")


Because 46 is the ASCII code of the dot. If I want, for example, tokenize the string with another character (which can be a letter, number, comma, semicolon, hash or any ASCII character from 1 to 255) with your code this is not possible because it is limited to only dot as a separator between tokens.

local text = "Roses-are-red-violets-are-blue"

As you can see, your code is unable to get "violets", when you just need enter 45 instead of 46.