Table Manipulation

<< Click to Display Table of Contents >>

Navigation:  Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > Function library > Table Manipulation >

Table Manipulation

Table Manipulation

mbul_mail table.concat

mbul_mail table.insert

mbul_mail table.maxn

mbul_mail table.remove

mbul_mail table.sort

This library provides generic functions for table manipulation. It provides all its functions inside the table table.

Most functions in the table library assume that the table represents an array or a list. For these functions, when we talk about the "length" of a table we mean the result of the length operator.

table.concat

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

Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j]. The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table. If i is greater than j, returns the empty string.

Example

local Array={10,20,30}

local Res=table.concat(Array,"-");

--Res=10-20-30

table.insert

table.insert (table, [pos,] value)

Inserts element value at position pos in table, shifting up other elements to open space, if necessary. The default value for pos is n+1, where n is the length of the table (see The Length Operator ), so that a call table.insert(t,x) inserts x at the end of table t.

Example

local Array={10,20,30}

table.insert(Array,40); --40 is added to the table end

table.insert(Array,2,50); --50 is added to the second table element, and all other elements are shifted upwards

table.maxn

table.maxn (table)

Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.)

Example

local Array={10,20,30}

local Count=table.maxn(Array);

--Count=3

table.remove

table.remove (table [, pos])

Removes from table the element at position pos, shifting down other elements to close the space, if necessary. Returns the value of the removed element. The default value for pos is n, where n is the length of the table, so that a call table.remove(t) removes the last element of table t.

Example

local Array={10,20,30}

local RemoveValue=table.remove(Array,2);

--the element number 2 is removed from the table; RemoveValue=20

table.sort

table.sort (table [, comp])

Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the length of the table. If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given, then the standard Lua operator < is used instead.

The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.

Example

--comparison function. It can be as complex as required.

function Compare(val1,val2)

 return val1<val2; --true if val1<val2; otherwise, false

end;

--use of sorting

local Array={13,2,44,22,18}

table.sort(Array,Compare);

--Array[1] =2,Array[2] =13,Array[3] =18,Array[4] =22,Array[5] =44