1

If I have a table like this, how would I print all the values?

local Buyers = {
    {[Name] = "Birk", [SecName] = "Birk2nd", [ThirdName] = "Birk3nd"},
    {[Name] = "Bob", [SecName] = "Bob2nd", [ThirdName] = "Bob3nd"},
}

It should end up printing:

First Name: Birk
Second Name: Birk2nd
Third Name: Birk3nd

FirstName: Bob
Second Name: Bob2nd
Third Name: Bob3nd
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Birk
  • 191
  • 2
  • 12
  • The standard library doesn't provide an easy way to do this. Have you tried coding this yourself? – luther Dec 28 '20 at 19:16
  • Yeah i tried to code it myself otherwise I would not ask here, but i think i found a solution ```for k, data in pairs(Buyers) do print(data) for key, value in pairs(data) do print(key) print(value) end end``` – Birk Dec 28 '20 at 19:27
  • 1
    Your code that initialises `Buyers` will not behave as you expect. `Name`, `SecName` and `ThisdName` are variables, equal to `nil` if not previously initialised, not literals. Either enclose them in quotes, or remove the brackets. – Alexander Mashin Dec 29 '20 at 02:43
  • `local Buyers = { {["Name"] = "Birk", ["SecName"] = "Birk2nd", ["ThirdName"] = "Birk3nd"}, {["Name"] = "Bob", ["SecName"] = "Bob2nd", ["ThirdName"] = "Bob3nd"}, }` for the Buyers table – Blanket Fox Dec 29 '20 at 07:02

2 Answers2

2

what i can think of

local Buyers = {
  {["Name"] = "Birk", ["SecName"] = "Birk2nd", ["ThirdName"] = "Birk3nd"},
  {["Name"] = "Bob", ["SecName"] = "Bob2nd", ["ThirdName"] = "Bob3nd"},
}

for _, person in pairs(Buyers) do
  print("First name: "..person.Name)
  print("Second name: "..person.SecName)
  print("Third name: "..person.ThirdName)
  print()
end
Blanket Fox
  • 377
  • 4
  • 15
0

For your case, like this:

local function serialise_buyer (buyer)
    return ('First Name: ' .. (buyer.Name or '')) .. '\n'
        .. ('Second Name: ' .. (buyer.SecName or '')) .. '\n'
        .. ('Third Name: ' .. (buyer.ThirdName or '')) .. '\n'
end

local Buyers = {
    {Name = "Birk", SecName = "Birk2nd", ThirdName = "Birk3rd"},
    {Name = "Bob", SecName = "Bob2nd", ThirdName = "Bob3rd"},
}

for _, buyer in ipairs (Buyers) do
    print (serialise_buyer (buyer))
end

A more generic solution, with sorting:

local sort, rep, concat = table.sort, string.rep, table.concat

local function serialise (var, sorted, indent)
    if type (var) == 'string' then
        return "'" .. var .. "'"
    elseif type (var) == 'table' then
        local keys = {}
        for key, _ in pairs (var) do
            keys[#keys + 1] = key
        end
        if sorted then
            sort (keys, function (a, b)
                if type (a) == type (b) and (type (a) == 'number' or type (a) == 'string') then
                    return a < b
                elseif type (a) == 'number' and type (b) ~= 'number' then
                    return true
                else
                    return false
                end
            end)
        end
        local strings = {}
        local indent = indent or 0
        for _, key in ipairs (keys) do
            strings [#strings + 1]
                = rep ('\t', indent + 1)
               .. serialise (key, sorted, indent + 1)
               .. ' = '
               .. serialise (var [key], sorted, indent + 1)
        end
        return 'table (\n' .. concat (strings, '\n') .. '\n' .. rep ('\t', indent) .. ')'
    else
        return tostring (var)
    end
end

local Buyers = {
    {Name = "Birk", SecName = "Birk2nd", ThirdName = "Birk3rd"},
    {Name = "Bob", SecName = "Bob2nd", ThirdName = "Bob3rd"},
    [function () end] = 'func',
    [{'b', 'd'}] = {'e', 'f'}
}
print (serialise (Buyers, true))
Alexander Mashin
  • 3,892
  • 1
  • 9
  • 15
  • i think it too overkill – Blanket Fox Dec 29 '20 at 07:00
  • too much complex – Blanket Fox Dec 29 '20 at 07:01
  • No matter how you look at it, that's way too much code. Also, don't localize variables for the fun of it, that just makes the code less readable and confuses IDEs. – DarkWiiPlayer Dec 29 '20 at 07:45
  • @DarkWiiPlayer localizing a functions can have a benefit to performance. It is covered in Lua Gems performance tips. here is a SO question on the topic: https://stackoverflow.com/questions/32945039/why-are-localized-functions-faster-in-lua – Nifim Dec 30 '20 at 15:34
  • A better example SO question and answers: https://stackoverflow.com/questions/18093728/is-there-any-performance-value-in-creating-local-copies-of-lua-functions – Nifim Dec 30 '20 at 15:41
  • @Nifim Try benchmarking the code with and without that "optimization" and you will find no difference. The performance increase of localizing variables would only be noticeable in hot loops or very simple functions, which this one (it even sorts an array) definitely isn't. – DarkWiiPlayer Dec 31 '20 at 08:19
  • also why you need sort it – Blanket Fox Jan 05 '21 at 08:42