Nested Tables

Posted by Jackson on Tue 18 Nov 2014 09:05 PM — 3 posts, 13,217 views.

#0
So Im capturing the room description and i read through the two threads from nick on lua tables but I dont understand how to edit the styles that I have saved to a table. I didnt see much on nested tables. cause all I have figured out just shows me another table.

table: 030CBFD0 1

So I saved the room

line, wildcards, styles = wait.match ("*")

Then I insert it into the table

table.instert(room, styles)


but now I want to be able to go through the tables and say edit the text or the textcolor in the nested tables. And I dont know how to do that. Say I want to go through the text and remove everything inside a [] and add a - to the start of each line. how is that done. Or Say I want to look at the text from key 23. Thanks


<aliases>
  <alias
   match="test2"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>require "wait"
require "tprint"



wait.make (function ()

local room = {}

Send ("Look")

local w = wait.match ("[ Exits: n e s w u d ]")

while true do

line, wildcards, styles = wait.match("*")

if not string.match (line, "[A-Za-z0-9]") then

break 


end


table.insert (room, styles)

end  -- end loop

tprint(room)

end)</send>
  </alias>
</aliases>
#1
So I figured out my answer, though im not exactly sure what the difference is in the for loops

for i goes through the Keys, but then what does for _ go and do?



for i, styles in ipairs (room) do
 
for _, style in ipairs (styles) do

style.text = style.text:gsub("%s%s+", " ")

print(style.text)
USA Global Moderator #2
ipairs gives a loop over two things together: index number and value at that index.

In your first loop, i is assigned the index number, and styles is assigned the value (in this case another table) at that index.

_ is the conventional way of saying "I don't care about this one so I'll give it a name that I won't ever want to use like a single underscore."