3

can someone help me with string concatenate problem. I read data from register. It's function utf(regAddr, length). I get table with decimal numbers, then I transform it into hex and to string in loop. I need concatenate these strings into one. there is not in Lua something like .= operator

function utf(regAddr, length)
  stringTable = {} 
  table.insert(stringTable, {mb:readregisters(regAddr-1,length)})

  for key, value in pairs(stringTable) do
    for i=1, length do
      v = value[i]
      v = lmcore.inttohex(v, 4)
      v = cnv.hextostr(v)   
      log(v)
    end  
  end
end

-- function(regAddr, length)
utf(30,20)

enter image description here

2
  • why are you inserting a table into stringTable for each item, why not table.insert(stringTable, mb:readregisters(regAddr-1,length))? Commented Feb 14, 2014 at 16:33
  • Hello, Im inserting a table into stringTable only once. its not in loop. I can also read this register as value1, value2, value3, ... , value20 = mb:readregisters(regAddr-1,20) but I think better is work with table and loop. I don`t want to have 20 variables. I need iterate via table and for each variable in table use lmcore.inttohex and cnv.hextostr to get ASCII string. Final text must be "Power Meter" in one variable. Commented Feb 14, 2014 at 16:43

3 Answers 3

7

There is no append operator for strings. Strings are immutable values.

The .. operator concatenates two string, producing a third string as a result:

local b = "con"
local c = "catenate"
local a = b .. c  -- "concatenate"

The table.concat function concatenates strings in a table, producing a string result:

local t = { "con", "catenate" }
local a = table.concat(t)  -- "concatenate"

local t = { "two", "words" }
local a = table.concat(t, " ") -- "two words"

The string.format function takes a format pattern with a list of compatible values, producing a string result:

local b = 2
local c = "words"
local a = string.format("%i %s", b, c)  -- "2 words"

local t = { 2, "words" }
local a = string.format("%i %s", unpack(t))  -- "2 words"

If you are accumulating a lot of strings that you eventually want to concatenate, you can use a table as a temporary data structure and concatenate when you are done accumulating:

local t = {}
for i = 1, 1000 do
    table.insert(t, tostring(i))
end
local a = table.concat(t) -- "1234...9991000"

For a very large number of strings, you can concatenate incrementally. See LTN 9: Creating Strings Piece by Piece and related discussions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Tom, your last piece of code works well and helped me
3

You should try the table.concat method.

Maybe this other question can help you:

1 Comment

Hi, I tried this function but for functions lmcore.inttohex and cnv.hextostr I need number no table. Some kind of append operator in loop seems to be solution, but I!m not sure how to do it
0

this code works:

function utf(regAddr, length)
    stringTable = {}
    table.insert(stringTable, {mb:readregisters(regAddr-1,length)})

    for key, value in pairs(stringTable) do
        t = {}

        for i=1, length do
            v = value[i]
            v = lmcore.inttohex(v, 4)
            v = cnv.hextostr(v)

            table.insert(t, v)
        end
        a = table.concat(t)

    end

end

-- function(regAddr, length)
utf(30,20)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.