Lua Logo
Functions in the Table table

table.concat (table [, sep [, i [, j]]])
table.foreach (table, f)
table.foreachi (table, f)
table.getn (table)
table.insert (table, [pos,] value)
table.remove (table [, pos])
table.setn (table, n)
table.sort (table [, comp])

nach obentable.concat (table [, sep [, i [, j]]])

Concatenate / connect the values of a table with..

Parameter:

The table which values has to be concatenated
A string or value which has to be placed between. (optional: default is an empty string)
From where the values should be connected (optional: default is 1)
Up to where the values have to be concatenated. (optional: the size of the table is default)

We remind of that strings and numbers can be concatenated with two dots.
( "a" .. 5 --> "a5" )
This function does the same with the values of a numerical table

Example:

myTable = {
19,
"iron",
10,
"clay",
}
result = table.concat(myTable)
print(result) --> 19iron10clay

result = table.concat(myTable, " space ")
print(result) --> 19 space iron space 10 space clay

result = table.concat(myTable, " space ", 2)
print(result) --> iron space 10 space clay

result = table.concat(myTable, " space ", 2, 3)
print(result) --> iron space 10


nach obentable.foreach

This function goes through every index of the table and is assigning it to the function (the second parameter inside the bracket).

Considering following parameters for return:
  • The table, in which every index has to be passed
  • The function who will get the value of the according index.

table.foreach (myTable , MyFunction)


Example:

myTable = {}
myTable.value1 = true
myTable.value3 = 8
myTable[1] = "Name1"
myTable[7] = "Name7"

function PrintSomething(_index)
print( _index, myTable[_index] )
end

table.foreach( myTable, PrintSomething )







nach obentable.foreachi

This function is analogous to the preceding, but she goes through only numerical indexes. But only as long as an order will be interrupted.

Considering following parameters for return:
  • The table, in which every numerical index has to be passed
  • The function who will get the value of the according index.

table.foreachi (myTable , MyFunction)


Example:

myTable = {}
myTable.value1 = true
myTable.value3 = 8
myTable[1] = "Name1"
myTable[7] = "Name7"

function PrintSomething(_index)
print( _index, myTable[_index] )
end

table.foreachi( myTable, PrintSomething )





As you can see, index 7 will not be printed as the numerical indices 2 up to 6 are missing.



nach obentable.insert

This function puts an value at the end of a table. If an exact position will be defined, the value
will be placed on this position and all following indices will be scrolled accordingly.
The only task of this function is, to change the table directly.

Considering following parameters for return:
  • They table in which the variable has to be stored
  • Optional the position where the variable should be placed exactly

table.insert (myTable , [position,] value)


Example:

myTable = {}
table.insert (myTable , 5)
result --> = myTable[1] == 5
-- again:
table.insert (myTable , "Luna")
result --> = myTable[1] == 5
myTable[2] == "Luna"
-- again:

table.insert (myTable , 2, true)
result --> = myTable[1] == 5
myTable[2] == true
myTable[3] == "Luna"


nach obentable.getn

This function returns the size of a numerical table.

Considering following parameters for return:
  • The table, which size we want to know

tableSize = table.getn(myTable)

Example:

myTable={ 1, 3, 6, "great", false, { 8,9} )

tableSize = table.getn ( myTable ) --> tableSize == 6
tableSize = table.getn ( myTable[6] ) --> tableSize == 2



nach obentable.remove

This function deletes an element of a numerical table and moves up the remaining indices if necessary.

Considering following parameters for return:
  • The table in which we want to delete an element
  • The position of this element (optional)
If the position will not be assigned, the last element of the table will be deleted.

deletedElement = table.remove(myTable[, pos])

Example:

myTable = { 1, 3, 6, "great", false, { 8,9} )
myTable[3] == 6
deletedElement = table.remove(myTable, 3)

From this it follows -->


myTable[3] == "great"
deletedElement == 6


nach obentable.setn

This function changes a size of a numerical table.

Considering following parameters for return:
  • The table which size should be changed
  • The size of the table afterwards

table.setn (myTable , 7)


Example:
As shown above at table.foreachi nothing will be printed besides index 1

myTable = {}
myTable.value1 = true
myTable.value3 = 8
table.setn(myTable,7)
myTable[1] = "Name1"
myTable[7] = "Name7"

function PrintSomething(_index)
print( _index, myTable[_index] )
end

table.foreachi( myTable, PrintSomething )




As we can see, all elements up to 7 will be printed.
For sure, it is also possible to reduce the size of a table with this function. However, this also means that all elements above the highest index will be deleted.






nach obentable.sort (table [, comp])

Arranges a numerical table ( myTable[1] ... myTable[n] )

Parameter:

  • The table which has to be arranged.
  • a function which defines the sort sequence.
If comp is not given, it will be sorted according the `<�������

��e:

myTable = {}
myTable[1] = 77
myTable[2] = 8
myTable[3] = 717
myTable[4] = 55

table.sort(myTable)

From this it follows:

myTable[1] --> 8
myTable[2] --> 55
myTable[3] --> 77
myTable[4] --> 717

____________________________________________________________________

or

myTable = {}
myTable[1] = "z"
myTable[2] = "o"
myTable[3] = "f"
myTable[4] = "n"


table.sort(myTable)

From this it follows:

myTable[1] --> "f"
myTable[2] --> "n"
myTable[3] --> "o"
myTable[4] --> "z"

____________________________________________________________________

All values have to be same type. Otherwise an error occurs: attempt to compare string with number

A table with non numerical indices can not be sorted directly. However, to do so all indices have to be written in a numerical table at first.
This table can be sorted afterwards.

Example:

myTable = {}
myTable.Z = 89
myTable.X = 8
myTable.Y = 55

sortTable = {}

for index in pairs(myTable) do
table.insert(sortTable, index)
end

sortTable[1] --> Y
sortTable[2] --> X
sortTable[3] --> Z

now the sortTable can be arranged accordingly

table.sort(sortTable)

sortTable[1] --> X
sortTable[2] --> Y
sortTable[3] --> Z

A function to do an arrangement:

As second parameter in table.sort a function can be assigned which defines the sort sequence.
She gets two values of the table as parameter. Then this function has to determine which of these parameters comes at first.
If the first value should come before the second, the function has to return true. Otherwise nil or false.

Example:

myTable = {
10,
124,
10,
48,
}

function comp(w1,w2)
if w1 > w2 then
return true
end
end

table.sort(myTable,comp)

with this the sort sequence will be changed

myTable[1] --> 124
myTable[2] --> 48
myTable[3] --> 10
myTable[4] --> 10

Copying of any content of this site (text or graphics) is not allowed, excepted any source code shown in this tutorial. See also: ....Disclaimer
Copyright © Robert Schmitz 2006