lua-users home
lua-l archive

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


I created various functions for string manipulation in Lua, where the strings are composed of tokens separated by an ASCII character (which may be the point, comma, semicolon etc.). Two of these are the functions numtok() and gettok(). Here is the code: http://nopaste.linux-dev.org/?824658

And this is the way it should work:

gettok(sInput, iPosition, Separator, iRange)

Where

    sInput = string to manipulate
iPosition = position of the token inside the string. If lesser than 0, it will be considered the position from the last token to the first. If equal to 0, returns the whole string.
    Separator = ASCII code of the token separator
iRange = optional: if specified, returns the token from position to range. If equal to 0, return all tokens from position to the end of the string.

local text = "apple.banana.cherry.grape.orange"

    gettok(text,1,46)
        apple

    gettok(text,-2,46)
        grape

    gettok(text,2,46,4)
        banana.cherry.grape

    gettok(text,-1,46,-3)
        cherry.grape.orange


Could you give me some advice on improving the code? In particular, I need your help to figure out how I can avoid writing each time the same piece of code (line 23 to 28, line 39 to 44, line 48 to 53) without using, as much as possible, the tables.

Can you help me?