[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Constructing tables in tables
- From: "Henderson, Michael D" <michael.d.henderson@...>
- Date: Wed, 23 Feb 2005 13:53:54 -0700
Hello. I'm using Lua 5.0.2 and I'm having trouble determining how to create nested tables. I'd looked at Chapter 10, Section 1 of Programming in Lua and was able to create a simple data description with a nested table like this
--==========================================
function comment(o)
return o;
end
function data(o)
print(" info: using following files...");
for idx,file in ipairs(o.fileList) do
print(" `" .. file .. "'");
end;
end
--==========================================
data {
id = "one";
fileList = { "aaa" , "bbb" , "ccc" };
comment {
author = "mike";
text = [[2005.02.20 - updated]];
};
};
I'd like to be able to have an array of the comments, but I can't figure out how to get there. How can I create one that allows me to have multiple instances of the table so that I can specify something like this
data {
id = "one";
fileList = { "aaa" , "bbb" , "ccc" };
comment {
author = "mike";
text = [[2005.02.20 - updated]];
};
comment {
author = "john";
text = [[2005.01.23 - created]];
}
};
It seemed to me that the function data(o) isn't called until the end of the data item. I'd thought that I could create a global (call it commentList) and tag each comment onto it
commentList = {}; -- empty array
idx = 0;
function comment(o)
commentList[idx] = o;
idx = idx + 1;
end
function data(o)
o.comments = commentList; -- copy array
commentList = {}; -- empty array
idx = 0;
print(" info: found " .. o.comments.getn .. " comments\n");
print(" using following files...");
for idx,file in ipairs(o.fileList) do
print(" `" .. file .. "'");
end;
end
But that's not working. When I try treating o.comments as an array, I get an error (attempt to concatenate field `getn' (a nil value)).
Am I approaching this completely wrong?
Thanks,
Mike