lua-users home
lua-l archive

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

On 11/03/2014 19:25, wrote:


(using Lua 5.2.2 under Linux)

from a serial line I receive A LOT of lines
consisting of data in ASCII format.

The first one to three characters decide, how
I have to interpret the data.
Since data are transmitted FAST I want to make
the recognition code which branches into the
different data processing parts as efficient
as possible. The code will run on a embedded
system, so I will not have the memory for huge
look-up table or such...
And I will do it in pure Lua since this is the
reason why I do this in pure Lua! ;)

What is the "best" solution?
('best' in "" since this also a product of the
point of view ;)

For simplicity sake, let it always be 3 characters to decide.
Define as many 3-character identifiers as you need.
Then it is a simple if-else:

    local prefix, info = line:sub(1,3), line:sub(4)
    if prefix == "abc" then
      -- do something
    elseif prefix == "123" then
      -- do something else
    elseif ...

(Strings equality test is very cheap in Lua.)