Hello, I'm working on remember what I learn about lua a while ago. So decide to create some functions (toke this idea from Garry's mod) and I ran into a problem. I was wondering if anyone be willing to tell me what I am doing wrong. I got this output (sorry tabs disappeared) when I ran my code (below it):
Table =
{
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
10 = 10
name = will
lua: functions.lua:24: attempt to concatenate local 'v' (a table value)
stack traceback:
functions.lua:24: in function 'WriteTable'
functions.lua:34: in main chunk
[C]: ?
[code]
function WriteTable(name,_table,level)
if level == nil then level = 1 end --how many tabs
--make it neater looking
local x = 1;
while x > level do
io.write("\t");
x = x + 1;
end
x = 1; --useing x later in loop
io.write(name .. " =\n{\n");--write the name
--Main loop
for k, v in pairs(_table) do
x = 1; --reset x
if type(k) == "table" then
WriteTable(k,_table[k],level + 1);
else
while x <= level do
io.write("\t")
x = x + 1;
end
io.write(k .. " = " .. v .. "\n");
end
end
io.write("}\n"); -- end of table
end
test = {1,2,3,4,5,6,7,8,9,10};
test.name = "will";
test.grades = {95,96,100}
--for k, v in pairs(test.grades) do
--print(k .. " = " .. v);
--end
WriteTable("Table",test);
os.execute("PAUSE");
[/code]
From what I can gather it is calling a error when trying to write the value from a key in test.grades. But I try just printing the key and values (before function call) of that table and it works fine.
Never mind I found the answer. For people who want to know it, when I was doing type(k) was doing the key so it was either a number or string. But when I did type(_table[k]) it return the type instead what the key was (ie number or string). Thanks for everyone would toke their time to read this
Sorry, you need to Log In to post a reply to this thread.