• 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 concerned with tables.

Hermes

dziwki kola gramy w lola
Joined
Nov 17, 2007
Messages
1,867
Reaction score
14
Location
Poland
Hi. There's following situation:
Code:
local table = {
	{1, 2} = {firstNumber = 150, secondNumber = 20},
	{3, 4} = {firstNumber = 120},
	{5, 6} = {secondNumber = 80}
}

I need function, that will return sum of firstNumber and secondNumber according to value, where "value" matches one of the values in first tables (in this case values are 1 or 2, 3 or 4, 5 or 6).

And what is the best way to do such operations on tables? I mean, to search through multiple tables that are relative to different values?

Thanks in advance & regards and stuff :p,
Hermes
 
@up
Aff, I think you should leave this forum if you want to write such posts here, it's not hard :)
My question is about certain situation and I have no idea how to do that, so I am asking. I know lua enough to write scripts freely, but there are some conditions that I have no idea about.
 
@up
Aff, I think you should leave this forum if you want to write such posts here, it's not hard :)
My question is about certain situation and I have no idea how to do that, so I am asking. I know lua enough to write scripts freely, but there are some conditions that I have no idea about.

Well i guess you don't know enough Lua to write scripts freely..

That is why i suggested that you "learn" Lua.

Its called constructive criticism, knowing how to make modifications to a script is not knowing the language.

As you stated there are certain situations you don't have any idea about, by going over just the basics of lua you will find the solution to your problem.

Learn how to trouble shoot an issue instead of giving up and looking to others for the answers.
 
Code:
local value = 1
local table = {
	{{1, 2}, 150, 20},
	{{3, 4}, 120},
	{{5, 6}, 80}
}

for _, v in ipairs(table) do
	if isInArray(v[1], value) then
		return v[2] + (v[3] or 0)
	end
end
 
Lua:
local t = {
	[{1, 2}] = {firstNumber = 150, secondNumber = 20},
	[{3, 4}] = {firstNumber = 120},
	[{5, 6}] = {secondNumber = 80, 343, shitNumber = 111}
}

local search = 1

for k, list in pairs(t) do
    if(isInArray(k, search)) then
        local sum = 0
        for _, v in pairs(list) do
            if(tonumber(v)) then
                sum = sum + v
            end
        end
    end
end
 
Back
Top