Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
[solved] iterating throw multiple levels of a table — Gideros Forum

[solved] iterating throw multiple levels of a table

jack0088jack0088 Member
edited March 2012 in General questions
I have a Xml parser which gives me a really deep structured table - but each level is based on same structure
Attributes = {
     id = "main"
}
Name = "g"
ChildNodes = {
     [1] = {
          Attributes = { id = "FredFeuerstein" }
          Name = "g"
          ChildNodes = {
               [1] = {
                  Attributes = {
                      id = "ShapeOfFred"
                  }
                  Name = ""
                  ChildNodes = {}
               }
               [2] = {...}
          }
     }
     [2] = {
          Attributes = { id = "Lol" }
          Name = "..."
          ChildNodes = {...}
     }
}
As you can see it has this structure, which is nested within each ChildNodes={}
table = {
   Attributes = {}
   Name = ""
   ChildNodes = {...}
}
How can I iterate through all of the items and make a new table out of it, that look similar to this:
newTable = {
     main = {
          FredFeuerstein = {
               ShapeOfFred = { ... }
          }
          Lol = { ... }
     }
}
Basically I want to take the id from Attributes in each level of depth and if it has ChildNodes then subordinate them into the newly created node in my newTable. Do you understand how I mean this? I just want to iterate through the whole big table with all those depths (which can be a lot: at least 2 levels) and take out the id's.

I was able to iterate through 2 levels of depth, but not all... I didn't managed to get a recursive function.. I don't know why...

How can I do this in Lua? I tried a lot and googled for 2 days, but it seems that I'm to stupid at the moment for this task, so please help!

--cheers
jack0088
Owltwins. Smart design and creative code.
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!

Comments

  • ndossndoss Guru
    edited March 2012
    I'm not sure if I totally understand what you need, but would this work?
    function processTable(t,out)
       if out == nil then out = {} end
       if t and t.Attributes and t.Attributes.id then
          out[t.Attributes.id] = t
          if t.ChildNodes then
             for i,child in ipairs(t.ChildNodes) do
                processTable(child,t)
             end
          end
       end
       return out
    end
     
    newTable = processTable(myTable)
    Given this table:
    myTable = {
       Attributes = { id = "main" },
       Name = "g",
       ChildNodes = {
          {
             Attributes = { id = "FredFeuerstein" },
             Name = "g",
             ChildNodes = {
                {
                   Attributes = { id = "ShapeOfFred" },
                   Name = "myname",
                }
             }
          },
          {
             Attributes = { id = "Lol" },
             Name = "...",
          }
       }
    }
    You'd be able to access ShapeOfFred's name like so:
    print("======>", newTable.main.FredFeuerstein.ShapeOfFred.Name)
    Here's the printout of newTable using inspect.lua:
    <1>{
      main = <2>{
        Attributes = <img class="emoji" src="http://forum.giderosmobile.com/resources/emoji/heart.png" title="<3" alt="<3" height="20" />>{
          id = "main"
        },
        ChildNodes = <4>{ <5>{
            Attributes = <6>{
              id = "FredFeuerstein"
            },
            ChildNodes = <7>{ <8>{
                Attributes = <9>{
                  id = "ShapeOfFred"
                },
                Name = "myname"
              } },
            Name = "g",
            ShapeOfFred = <table 8>
          }, <10>{
            Attributes = <11>{
              id = "Lol"
            },
            Name = "..."
          } },
        FredFeuerstein = <table 5>,
        Lol = <table 10>,
        Name = "g"
      }
    }

    Likes: atilim

    +1 -1 (+1 / -0 )Share on Facebook
  • Can I have it somehow building the new table like:
    main = {
              FredFeuerstein = {
                   ShapeOfFred = { ... }
              }
              Lol = { ... }
         }
    So only the id's get nested in each other and no other data from the original table?


    PS:how did you get the self references into the table?
    Thank you very much so far and sorry, for my lack of knowledge...
    Owltwins. Smart design and creative code.
    »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
  • Got it :) thank you for your amazing recursive function!

    Likes: gorkem

    Owltwins. Smart design and creative code.
    »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.