Setting Table Items

Here is an example on how you can use a very powerful table manipulation function: TB.Set().

The purpose of this function is to set (recursively) a destination table with the items on a source table. If destination table have an item found on the source table it will be overwritten.

This is particurarly useful if you have large tables and you need to set only a subset.

Function TB.TEST_Set()
  Local table = { name     = "Fabio Falcucci", 
                  age      = 44, 
                  hobbies  = "music, programming, videogames, science",
                  job      = "programmer",
                  gender   = "male",
                  location = "Italy",
                  aspect   = 
                    { eyes   = "brown",
                      height = "1.75m",
                      hair   = "few",
                      weight = "too much" },
                  }
                  
  NPrint("\nTABLE CONTENT")
  For i, v In Pairs(table)
    If GetType(v) = #TABLE
      NPrint("[Color=#GREEN]" .. i .. " : [/color] sub-table")
      For ii, vv In Pairs(v)        
        NPrint("   [Color=#GREEN]" .. ii .. " : [/color]" .. vv)
      Next
    Else
      NPrint("[Color=#GREEN]" .. i .. " : [/color]" .. v)
    EndIf
  Next
  
  NPrint("\nChanging contents using TB.Set()")
  NPrint("In red all changed items.\n")
  TB.Set(table, { job = "[color=#RED]programmer, consultant[/color]", 
                  name = "[color=#RED]Fabio Falcucci (Allanon)[/color]",
                  aspect = { hair = "[color=#RED]none[/color]" }
                  }, 
                  False)
  
  NPrint("\nUPDATED TABLE CONTENT")
  For i, v In Pairs(table)
    If GetType(v) = #TABLE
      NPrint("[Color=#YELLOW]" .. i .. " : [/color] sub-table")
      For ii, vv In Pairs(v)        
        NPrint("   [Color=#YELLOW]" .. ii .. " : [/color]" .. vv)
      Next
    Else
      NPrint("[Color=#YELLOW]" .. i .. " : [/color]" .. v)
    EndIf
  Next

  NPrint("\nLeft mouse to QUIT.")
  
  WaitLeftMouse()
  
EndFunction
hollywoodlibs/tables/testset.txt · Ultima modifica: 2016/10/21 09:37 da admin
CC Attribution-Noncommercial 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0