• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Question about tables inside tables etc..

dudeim

Member
Joined
Oct 5, 2007
Messages
121
Reaction score
9
Hey,

I want to know how to use tables inside tables.
Here is a script and I want to know if there is a correct way in there or I need to use another way

Lua:
ExampleTable = { Example1 = { Example2 } }
-- ok so now I can have something like: ExampleTable.Example1.Example2 right?
function example()
for i = 1,100 do
    for j = 1,10 do
        ExampleTable.Example1[i].Example2[j] = i+j --this line is this possible or do I need to set it up otherwise/use table.insert first?
    end
end

function example2()
for i = 1,100 do
    for j = 1,10 do
        if ExampleTable.Example1[i].Example2[j] == nil then -- so this checks if the value exists and if it doesn't it inserts the value in the appropiate key and if it already exists then it just changes the value to the new value
           table.insert(ExampleTable.Example1[i].Example2[j],i+j)
        else
           ExampleTable.Example1[i].Example2[j] = i+j
        end
    end
end
So is the function example or example2 correct? Or do I need another way to do this?

Thanks!
 
I usually avoid naming anything inside the subtables unless it's a config table.

Neither would work and you would end up with attempt to index field ? errors because Lua *requires* you to create the parent table before trying to call or use its child index.

Code:
ExampleTable = { Example1 = { Example2 } }
for i = 1,100 do
	[B][COLOR="red"]ExampleTable.Example1[i] = {}[/COLOR][/B]
	for j = 1,10 do
		[B][COLOR="red"]ExampleTable.Example1[i].Example2 = {}[/COLOR][/B]
		ExampleTable.Example1[i].Example2[j] = i+j --this line is this possible or do I need to set it up otherwise/use table.insert first?
	end
end
 
Last edited:
I usually avoid naming anything inside the subtables unless it's a config table.

Neither would work and you would end up with attempt to index field ? errors because Lua *requires* you to create the parent table before trying to call or use its child index.

Code:
ExampleTable = { Example1 = { Example2 } }
for i = 1,100 do
	[B][COLOR="red"]ExampleTable.Example1[i] = {}[/COLOR][/B]
	for j = 1,10 do
		[B][COLOR="red"]ExampleTable.Example1[i].Example2 = {}[/COLOR][/B]
		ExampleTable.Example1[i].Example2[j] = i+j --this line is this possible or do I need to set it up otherwise/use table.insert first?
	end
end

ok so this line:
Lua:
ExampleTable.Example1[i] = {}
basicly creates the Example2 table?
 
Powered by Google Docs
Lua and Perl also take different approaches to arrays. In Perl, if you index an element beyond
the size of the array, all intermediate elements are automatically created with undefined values.
In Lua, you can index beyond the size of the array, but the intermediate elements are not
created.
 
Back
Top