TList


The TList function lists the contents of a table or a simple value such as a string or number. This command is most useful for Lua tables. Every element of a table is listed in the format [key]=value. Nested, or multi-dimensional tables and data structures, including arrays, are supported. The difference between this function and List is that List prints only the array part of a table and does not list key names. For a quick look at an array's contents, use List.

Syntax

TList( value )

where

    value is a number, string, or table.

  

This function lists all table elements with {} around the table data. Each level of nested table is indented and bracketed with {} around the table content. This is shown in the examples below. Note that when retrieving a table having key names, rather than numeric array indicices, Lua does not necessarily return them in the order they appear in your table.

A table element entered as a value but with no key, is defined to be an array element and is automatically assigned a numeric index as its key. This is shown in the examples below. For example, in the first example, the first member of table t is the value 5 which has no explicit key. Therefore, 5 is automatically assigned array index 1 as can be seen in the script output.

Examples

The following script creates a complex table and lists its contents. Notice that this table has a nester array inside a nested table.

t = { 5, "My Name", A=1, 6, {x=1, y= {2,3,4}}, 7 }

-- create a table

TList( t )

-- list the table

This script produces the output shown below. Notice that the array components are listed with their array key, like 1, 2, or 3. The non-array components (or the "hash" part of the table) is listed with key names such as d and A. Note that A is listed out of order, but there's nothing that can be done about that. Array indices always are listed in order.

{

 [1]= 5

 [2]= string

 [3]= 6

 [4]=

 {

  [y]=

  {

   [1]= 2

   [2]= 3

   [3]= 4

  }

  [x]= 1

 }

 [5]= 7

 [A]= 1

}

 

In the next example, the table contains no key names. However, it contains a nested table. All entries are array indices.

t = { 5, 6, {20,30,40}, 7 }

-- create a table

TList( t )

-- list the table

The result is shown below. Note the nested table at array index [3]:

{

 [1]=5

 [2]=6

 [3]=

 {

  [1]=20

  [2]=30

  [3]=40

 }

 [4]=7

}

Related Topics

Contents

List

Printf

Table Functions