Maintenance for the week of April 15:
• PC/Mac: No maintenance – April 15
• ESO Store and Account System for maintenance – April 16, 8:00AM EDT (12:00 UTC) - 12:00PM EDT (16:00 UTC)
The issue is resolved, and the North American PC/Mac megaserver is now available. Thank you for your patience!

Help with nested tables

krachall
krachall
✭✭✭✭✭
Can someone point me in the right direction for using dictionary style tables as a database? I'm trying to organize data in tables that I can extract with some interative function (ipairs or a do loop) but I'm not sure how to set this all up.

Basically here's what I have

{NAME = "Item A", LENGTH = "25", WIDTH = "12", HEIGHT = "8", TYPE = "box"}
{NAME = "Item B", LENGTH = "11", WIDTH = "18", HEIGHT = "4", TYPE = "bag"}
{NAME = "Item C", LENGTH = "19", WIDTH = "11", HEIGHT = "2", TYPE = "bag"}
{NAME = "Item D", LENGTH = "12", WIDTH = "15", HEIGHT = "1", TYPE = "box"}
{NAME = "Item W", LENGTH = "20", WIDTH = "14", HEIGHT = "9", TYPE = "box"}

Then I want to able to cycle through each of these rows and pick out individual variables.

So maybe I want to add up all the WIDTH of everything that's a box. I'd need to iterate through each Item, check if it's TYPE = Box, and if it is, recall the WIDTH field.

Suggestios?
  • krachall
    krachall
    ✭✭✭✭✭
    Figured it out and it was far simpler than I expected.
  • Baertram
    Baertram
    ✭✭✭✭✭
    local myTab = {
    {NAME = "Item A", LENGTH = "25", WIDTH = "12", HEIGHT = "8", TYPE = "box"}
    {NAME = "Item B", LENGTH = "11", WIDTH = "18", HEIGHT = "4", TYPE = "bag"}
    {NAME = "Item C", LENGTH = "19", WIDTH = "11", HEIGHT = "2", TYPE = "bag"}
    {NAME = "Item D", LENGTH = "12", WIDTH = "15", HEIGHT = "1", TYPE = "box"}
    {NAME = "Item W", LENGTH = "20", WIDTH = "14", HEIGHT = "9", TYPE = "box"}
    }
    local totalWidth = 0
    for idx, dataTab in ipairs(myTab) do
      if dataTab.TYPE == "box" then
         totalWidth = totalWidth  + dataTab.WIDTH
      end
    end
    
  • krachall
    krachall
    ✭✭✭✭✭
    Baertram wrote: »
    local myTab = {
    {NAME = "Item A", LENGTH = "25", WIDTH = "12", HEIGHT = "8", TYPE = "box"}
    {NAME = "Item B", LENGTH = "11", WIDTH = "18", HEIGHT = "4", TYPE = "bag"}
    {NAME = "Item C", LENGTH = "19", WIDTH = "11", HEIGHT = "2", TYPE = "bag"}
    {NAME = "Item D", LENGTH = "12", WIDTH = "15", HEIGHT = "1", TYPE = "box"}
    {NAME = "Item W", LENGTH = "20", WIDTH = "14", HEIGHT = "9", TYPE = "box"}
    }
    local totalWidth = 0
    for idx, dataTab in ipairs(myTab) do
      if dataTab.TYPE == "box" then
         totalWidth = totalWidth  + dataTab.WIDTH
      end
    end
    

    Thanks. That's what I figured out and it works great. Far simpler than I thought it would be.

    Question though...what if you had a three-deep table? how do you extract that value? would it be Table1.table2variable.table3variable?
  • M0R_Gaming
    M0R_Gaming
    ✭✭✭
    Ya, pretty much. Or using indexes:
    local out = 0
    for table1ind=1,#table1 do
        local table2 = table1[table1ind]
        for table2ind=1,#table2 do
            local table3 = table2[table2ind]
            for table3ind=1,#table1[ta]
                if table3[table3ind].TYPE == "box" then
                    out = out + table3[table3ind].WIDTH
                end
            end
        end
    end
    d(out)
    

    After writing that, that looks like a nightmare and its prob not optimized lol


    Alternatively, for ipairs:
    local out = 0
    for i, table2 in ipairs(table1) do
        for j, table3 in ipairs(table2) do
            for k, table4 in ipairs(table3) do
                if table4.TYPE == "box" then
                    out = out + table4.WIDTH
                end
            end
        end
    end
    d(out)
    
    • PC/NA - PvP/PvE AD Magsorc main
    • Former Emp, All HMs but DSR
    My addons
  • Baertram
    Baertram
    ✭✭✭✭✭
    Check ZOs build in table functions helpers, they also provide stuff like "find entry where key == ... and return value, or iterate over with functionX ...

    Maybe it could help here:
    https://github.com/esoui/esoui/blob/master/esoui/libraries/utility/zo_tableutils.lua
Sign In or Register to comment.