How to concatenate strings in one loop?

can someone help me with string concatenation problem. I am reading data from the registry. It executes the utf (regAddr, length) function. I get a table with decimal numbers, then convert it to hex and string in a loop. I need to combine these lines into one. Lua doesn't have 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

+3
source share
3 answers

There is no add operator for rows. Strings are immutable values.

The operator ..concatenates two lines, resulting in a third line:

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

The function table.concatconcatenates the rows in the table, creating 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 function string.formataccepts a format template with a list of compatible values, creating the result of the string:

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"

, , , :

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

. . LTN 9: .

+5

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)
0
source

All Articles