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

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
user3159120
  • 75
  • 1
  • 1
  • 7
  • why are you inserting a table into stringTable for each item, why not `table.insert(stringTable, mb:readregisters(regAddr-1,length))`? – Oliver Feb 14 '14 at 16:33
  • Hello, I`m inserting a table into stringTable only once. it`s 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. – user3159120 Feb 14 '14 at 16:43

3 Answers3

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.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
3

You should try the table.concat method.

Maybe this other question can help you:

Community
  • 1
  • 1
Rodrigo Gauzmanf
  • 2,517
  • 1
  • 23
  • 26
  • 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 – user3159120 Feb 14 '14 at 17:31
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)
user3159120
  • 75
  • 1
  • 1
  • 7