• 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!

TFS 1.X+ Structuring string in Lua from 2 tables?

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
72
Recently I was trying to make better strings displayed to user. So here we have this code to make new table:

EX1
local table = {
InnerTable1 = {"str1", "str2", "str3"}
InnerTable2 = {"str5", "str6"}
}

EX2
local table = {
InnerTable1 = {"str1"}
InnerTable2 = {"str7", "str8", "str9"}
}

What you want to do with any of these tables now, is to structure them into 2 columns as such:

COL1 (will correspond to InnerTable1)---------------COL2 (InnerTable2)
str1-----------------------------------------------------str5
str2-----------------------------------------------------str6
str3-----------------------------------------------------

These columns represent string in the console (you know when you print messages), my issue is how would one actually achieve this goal. Its very easy to simply print one column after another but I am looking to have them printed side by side.

These InnerTables are not fixed size and they change, so its hard to predict how many times you should iterate 😊. I guess this is not a big issue because you can easily get the size. What I am struggling with is, trying to iterate over one InnerTable once and then I want to switch to another to concatenate the string - I guess that's part of the solution
 
Solution
Lua:
local size = #table.InnerTable1 > #table.InnerTable2 and #table.InnerTable1 or #table.InnerTable2
for i = 1, size do
    print(table.InnerTable1[i] and table.InnerTable1[i] or "", table.InnerTable2[i] and table.InnerTable2[i] or "")
end
Lua:
local size = #table.InnerTable1 > #table.InnerTable2 and #table.InnerTable1 or #table.InnerTable2
for i = 1, size do
    print(table.InnerTable1[i] and table.InnerTable1[i] or "", table.InnerTable2[i] and table.InnerTable2[i] or "")
end
 
Solution
Lua:
local size = #table.InnerTable1 > #table.InnerTable2 and #table.InnerTable1 or #table.InnerTable2
for i = 1, size do
    print(table.InnerTable1[i] and table.InnerTable1[i] or "", table.InnerTable2[i] and table.InnerTable2[i] or "")
end

Thank you! Hopefully I will be able to make such complex solutions myself in the future lol
 
Could be shortened and more readable:
Lua:
local t = {
    inner1 = {'a', 'b', 'c', 'd'},
    inner2 = {'e', 'f'}
}

for i = 1, math.max(#t.inner1, #t.inner2) do
    print(string.format('%s-------%s', t.inner1[i] or '', t.inner2[i] or ''))
end
 
Could be shortened and more readable:
Lua:
local t = {
    inner1 = {'a', 'b', 'c', 'd'},
    inner2 = {'e', 'f'}
}

for i = 1, math.max(#t.inner1, #t.inner2) do
    print(string.format('%s-------%s', t.inner1[i] or '', t.inner2[i] or ''))
end
You guys are amazing, thanks alot!
 
Back
Top