Table API

Functiontable.concat
Turn table into a string, provided that all of its elements can be coerced into a string. If a separator is specified, then it is inserted between each element. Additionally, concatenation starts at the ith element and ends at the jth.
Syntax table.concat(
  • table : table
  • separator? : string
  • i? : number
  • j? : number
)
Returns string
Part of Lua (source)
API table
ExampleConcat an array of numbers
Since numbers are coercible to strings, an array of numbers can be concatenated.
Code
<nowiki>
print(table.concat({ 1, 2, 3 }, ', '))
    </nowiki>
Output
1, 2, 3
ExampleConcat a slice of an array
Concatenates an array of both strings and numbers, starting from the 3rd element up to the 5th.
Code
<nowiki>
print(table.concat({ 1, 2, 3, "a", "b", 4, 5 }, ' ', 3, 5))
    </nowiki>
Output
3 a b
Functiontable.foreach
This function is deprecated.[1]

Calls func once for each key-value pair in the tbl. Specifically,

<nowiki>
table.foreach(tbl, func)
</nowiki>

is equivalent to

<nowiki>
for key, value in pairs(tbl) do
  if func(key, value) ~= nil then
    break
  end
end
</nowiki>
Syntax table.foreach(
  • tbl : table
  • func : function
)
Returns nil
Part of Lua (source)
API table
ExamplePrint the elements of a table
Print out each key-value pair in tbl.
Code
<nowiki>
local tbl = { a = 1, b = 2, 'foo', 'bar' }
table.foreach(tbl, function(k, v)
  print(k, v)
end)
    </nowiki>
Output
<nowiki>
a 1
b 2
1 foo
2 bar
</nowiki>


References

Table.foreachi

Functiontable.insert
Adds item to the table, at the specified position, or at the end if position is not specified.
Syntax table.insert(
  • table : table
  • position? : number
  • item : any
)
Returns nil
Part of Lua (source)
API table
ExampleAdd an item to a table
Adds Jens to the table.
Code
<nowiki>
local names = {"Markus","John","Tim"}
table.insert(names,"Jens")
print(textutils.serialize(names))
    </nowiki>
Output
{
    "Markus",
    "John",
    "Tim",
    "Jens",
}
ExampleAdd an item to a table at a certain position
Add Dinnerbone to the table at position 3.
Code
<nowiki>
local awesomeMinecraftPeople = {"jeb","Notch","dan200"}
table.insert(awesomeMinecraftPeople, 3, "Dinnerbone")
print(textutils.serialize(awesomeMinecraftPeople))
    </nowiki>
Output
{
    "jeb",
    "Notch",
    "Dinnerbone",
    "dan200",
}

Table.pack Table.remove

Functiontable.sort
Sort the array, using the function sorter to compare the elements, or < if it is not given, in place. If no sorter is specified, the array must consist entirely of comparable values (either numbers or strings, but not both).
Syntax table.sort(
  • table : table
  • sorter? : function
)
Returns nil
Part of Lua (source)
API table
ExampleSort an array
Print a sorted version of the array, which is turned into a string using table.concat.
Code
<nowiki>
t = { 5, 4, 2, 1, 3 }
table.sort(t)
print(table.concat(t, ', '))
    </nowiki>
Output
1, 2, 3, 4, 5
ExampleFind the smallest element in an array of random numbers
Instead of using a for loop to find the smallest element in an array, it is possible to sort and then index it. The first index will be the smallest element.
Code
<nowiki>
local elements = 10
local array = {}
for i = 1, elements do
  array[i] = math.random(1, 1000)
end
table.sort(array)
print(array[i])
    </nowiki>
Output The smallest number in array, which depends on how the array was filled.
ExampleSort an array of tables by their length
This demonstrates using a custom sorter function to compare tables by their length. Note that generally < will not compare tables.
Code
local array = { { 'foo', 'bar', 'baz' }, { 'red', 'green', 'blue' }, { 'apple', 'orange' } }
table.sort(array, function(a, b)
  return #a < #b
end)
print(array[1][1])
Output
'apple'
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.