1

I would like to store my data into a n*6 table, and the table would be like

Test1  Test2  Test3  Test4  Test5  Test6
1      abc    sss    efg    1000   ijk
2      cde    yyy    ghi    2000   klm
...
...       and so on
...

I have declare the table at first

local myTable = {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6"}

then how should I make n*6 table for storing my data with regular format?

any information will be appreciated!!

Tyo Yang
  • 81
  • 8
  • You mean a multi-dimensional table? Put tables within tables, like `local myTable = {{}, {}, {}, {}, {}, {}}` – Colonel Thirty Two Aug 13 '14 at 02:42
  • possible duplicate of [How do I make a 2D array in Lua?](http://stackoverflow.com/questions/8780798/how-do-i-make-a-2d-array-in-lua) – Retired Ninja Aug 13 '14 at 02:43
  • You have a file or string of such format, and you want to extract a table? Is that what you mean after the edit? – Yu Hao Aug 13 '14 at 02:56
  • yes, I got some string variables from text files, then I wanna to make it together as a n*6 table. how should I do? – Tyo Yang Aug 13 '14 at 03:00
  • First, make your question clear, as it's unclear right now. Second, the code you have has nothing to do with the real problem. Do you have any idea how to make a simple table out of a string, before asking about a complex one? – Yu Hao Aug 13 '14 at 03:09
  • Sorry for insufficient information, simple table refer `http://www.tutorialspoint.com/lua/lua_tables.htm`, i JUST wanna some suggestion or an idea to achieve this COMPLEX one. – Tyo Yang Aug 13 '14 at 03:27

3 Answers3

2

You can have a nested table:

local myTable = {
    {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6"},
    {1, "abc", "sss", "efg", 1000, "ijk"},
    {2, "cde", "yyy", "ghi", 2000, "klm"},
    -- and so on
}
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • thanks hjpotter92, and sorry for my bad expression, I got a regular format, and the data which parse out from log file is not expected. So, how should I input these data into the table? – Tyo Yang Aug 13 '14 at 02:52
  • You mean that table is in a separate file and you want to load the contents as a lua-table? – hjpotter92 Aug 13 '14 at 02:54
  • nop, i got the data which parse from some text files, and now I wanna put them to a table – Tyo Yang Aug 13 '14 at 02:58
2

This is an example code of how to extract data from a string and store into a table:

local str = 'Test1  Test2  Test3  Test4  Test5  Test6'
local myTable = {}
for s in str:gmatch("%S+") do
    myTable[#myTable + 1] = s
end

Your problem is a little more complex because it's a 2-dimensional array, but the general idea is similar, I'll leave that to you. Hint: Use a loop to get each line , and another loop to extract the data from each line.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • the data is unexpected, so I cannot declare it like this, but thanks!! – Tyo Yang Aug 13 '14 at 02:54
  • @TyoYang I've updated the answer. But first, make your question more clear, it's not clear at least to hjpotter and me because we both thought it was another problem at first. – Yu Hao Aug 13 '14 at 03:25
1

Here's an alternative, with the first column becoming a set of contiguous positive integer keys:

local myTable = {
    {Test2="abc", Test3="sss", Test4="efg", Test5=1000, Test6="ijk"},
    {Test2="cde", Test3="yyy", Test4="ghi", Test5=2000, Test6="klm"},
    -- and so on
}

It probably comes down to how you want to access the data. With a table like this, you can go:

-- assumes that all rows are kept together (i.e., myTable is a "Lua sequence")
for i = 1, #myTable do 
print(
    myTable[i].Test2,
    myTable[i].Test3,
    myTable[i].Test4,
    myTable[i].Test5,
    myTable[i].Test6)
end
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72