Page 1 of 1

add data to table

Posted: Saturday 19 October 2019 14:55
by pvklink
Hi,
Somebody knows how to add data/record to:
local testdata ='{"veld1": "waarde 1","veld2": "waarde 2"}'

for example by:
testdata.add("veld3": "waarde 3")

Code: Select all

return {
        active = true,
        on = {
            devices = {'test'}
        },
        data = {
            switchevents = { initial = {} }
        },
    execute = function(dz, item, info)


    local testdata  ='{"veld1": "waarde 1","veld2": "waarde 2"}'

    function deepdump( tbl )
            local checklist = {}
            
            local function innerdump( tbl, indent )
                checklist[ tostring(tbl) ] = true
                for k,v in pairs(tbl) do
                        print (indent .. tostring(k) .. " ===> value: ".. tostring(v)  )
                    if (type(v) == "table" and not checklist[ tostring(v) ]) then 
                        innerdump(v,indent .. tostring(k) .. ".") 
                    end
                end
            end
            checklist[ tostring(tbl) ] = true
            innerdump( tbl, "Key: " )
    end

    -- how to add "veld3": "waarde 3" to testdata
    testdata.add("veld3": "waarde 3")

    local myTable   = dz.utils.fromJSON(testdata) 
    deepdump(myTable) -- use this function call to see the structure of your table

end
}

Re: add data to table

Posted: Saturday 19 October 2019 18:33
by waaren
pvklink wrote: Saturday 19 October 2019 14:55 Somebody knows how to add data/record to:
There is more then one way to do this. Have a look at this:

Code: Select all

return {
        active = true,
        on = { devices = {'test', 'myTrigger'}},

    execute = function(dz, item)

    local testJSON  ='{"key1": "value 1","key2": "value 2"}'
    local testTable =   { 
                            ["key3"] = "value 3",
                            ["key4"] = "value 4",
                        }

    local myTable   = dz.utils.fromJSON(testJSON) 

    -- merge tables
    for k,v in pairs(testTable) do myTable[k] = v end

    local varName   = "key 8"
    local varName2  = { ["key 9"] = "value 9" }

    myTable.key5 = "value 5" -- method 1. Only allowed for string keys without spaces in name
    myTable["key 6"] = "value 6" -- method 2 (necessary when something else as a string without spaces)
    myTable[7] = "value 7" -- method 3 numeric key
    myTable[varName] = "value 8" -- method 4 using content of a variable as key
    myTable[9] = varName2 -- method 5 add a sub-table
    
    dz.utils.dumpTable(myTable) 
end
}


Re: add data to table

Posted: Monday 21 October 2019 9:58
by pvklink
Ok, Thanks!
two more questions :D
1. When i dont know the number of records in the table, is there a command to add it 'as the latest' (becasue you define a recordnumber) or do i read the table first and count the number of records?
2. the solution must have the option to store some names and values that belong together as a record like:
[{"ident": "aaaa", "var1": "1", "var2": "1"},{"ident": "zzzz", "var1": "2", "var2": "1"}]

and then add: ,{"ident": "bbbbb", "var1": "1", "var2": "7"}]

Re: add data to table  [Solved]

Posted: Monday 21 October 2019 17:29
by waaren
pvklink wrote: Monday 21 October 2019 9:58 1. When i don't know the number of records in the table, is there a command to add it 'as the latest' (because you define a record-number) or do i read the table first and count the number of records?
2. the solution must have the option to store some names and values that belong together as a record like:
[{"ident": "aaaa", "var1": "1", "var2": "1"},{"ident": "zzzz", "var1": "2", "var2": "1"}]

and then add: ,{"ident": "bbbbb", "var1": "1", "var2": "7"}]
dzVents (=Lua) tables are key/value containers but can also be treated like an array.
t = {"a", "b", "c"} This is a syntax shortcut for t = {[1]="a", [2]="b", [3]="c"}

1.
If your table is build like an array with only consecutive numbers starting at 1, you can query the number of records in the table with #tablename
If the table is build with other keys or with gaps in the numbering or not starting with 1 then you will need to loop through the table the get the number of records. with something like the getTablePairsCount function in the next answer

2.

Code: Select all

local function getTablePairsCount(t)
   local tableCounter = 0
   for k,v in pairs(t) do
       tableCounter = tableCounter + 1
   end
   return tableCounter
end

local function tableValuesToString(t,sep)
   local myString = '' 
   local sep = sep or ',' -- default separator = ,
   for k, v in pairs(t) do
        if type(v) == 'string' then
            myString = myString .. v .. sep 
        end
   end
   if myString ~= '' then  return myString:gsub("[" .. sep .. "]*$",'') end -- return concatenated string after removing last separator
end


local rTable = {} 
local record1 = '{"ident": "aaaa", "var1": "1", "var2": "1"}'
local record2 = '{"ident": "zzzz", "var1": "2", "var2": "1"}'
local record3 = '{"ident": "bbbbb", "var1": "1", "var2": "7"}'

rTable[1] =  record1

rTable[#rTable + 1] =  record2
rTable[#rTable + 1] =  record3

print ('there are ' .. getTablePairsCount(rTable) .. '  records in the table') -- works on all tables
print ('there are ' .. #rTable .. '  records in the table') -- gives only a valid answer on pure array like tables
print ('[' .. table.concat(rTable,',') .. ']') -- table concat only concatenates the values within the pure array part of the table (with key 1,2,3,..)

rTable[0] = '{"ident": "00000", "var0"=": "0", "var00": "00"}' 
rTable['negen'] =  '{"ident": "999999", "var9"=": "9", "var99": "99"}' 

print ('there are now ' .. getTablePairsCount(rTable) .. '  records in the table')
print ('there are now more then '  .. #rTable .. '  records in the table') -- #rTable does not give the count of all records now only the ones in the pure Array

print('\n\n')

print ('Values from pure array part of the table ==>>  [' .. table.concat(rTable,',') .. ']')  -- this does not print all the table values
print ('Values from complete table ==>> [' .. tableValuesToString(rTable,',') .. ']') 


Re: add data to table

Posted: Monday 21 October 2019 20:08
by pvklink
Thank you very much, something to play with the next coming weeks!
This is exactly what i meant!