[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: String tokenization function
- From: Marco Atzori <marco.atzori.1983@...>
- Date: Sat, 7 Nov 2015 15:42:53 +0100
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:
And this is the way it should work:
gettok(sInput, iPosition, Separator, iRange)
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
local text = "apple.banana.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?