List


The List function lists the contents of a variable list of objects consisting of numbers, strings, tables, etc. Tables may contain nested tables. Each value or table element is listed on a new line and table elements are intented at the level of the table.

Syntax

List( ... )

where

    ... is a list of numbers, strings, tables, or other Lua types.

  

This function uses the Lua pairs function to access all table elements, without requiring each succesive table index to exist. As a result, the table elements are listed in an arbitrary order. Note that you do not need to use integral values as the table indices (see "blarney" in Example 1, below).

Example 1

The following script creates a complex table and lists its contents. Of course, your tables need not be this complex:

local t={}

 

t[5] = TRand(3)

-- table at index 5 has 3 random numbers

t[3] = TRand(4)

-- table at index 3 has 4 random numbers

t[2] = {}

-- create a subtable at table index 2

t[2][1] = t[5]

-- subtable is identical to t[5]

t[2]["blarney"] = t[3]

-- subtable is identical to t[3]

t[6] = {"howdy","doody","time"}

-- subtable at index 6 has 3 strings

List(t,5,"My string")

-- List the table and 2 other values

The results from List() look as follows. Table element 3 is listed as [3], and so on. Each table level has a larger indent. Note that the table elements are not accessed in any particular order, which is normal when the table is not treated as an array:

 

[3]

{

  0.894742

  0.642079

  0.824183

  0.889676

}

[2]

{

  [1]

  {

    0.320719

    0.83578

    0.686666

  }

  [blarney]

  {

    0.894742

    0.642079

    0.824183

    0.889676

  }

}

[6]

{

  howdy

  doody

  time

}

[5]

{

  0.320719

  0.83578

  0.686666

}

5

My string

Example 2

The following script creates 2 tables and lists their contents.

 

X = TRand(5)

-- table of 5 random values between 0 and 1

Y = TRand(5,10,20)

-- table of 5 random values between 10 and 20

List(X,Y)

-- list the tables

The result from List() looks as follows. Note that each table has 5 elements:

0.1171

0.747124

0.616108

0.921049

0.203772

14.6348

17.5863

19.5666

16.9369

12.4473

Example 3

The following script creates 2 tables and lists their contents with a label above each table. This is the same script as in example 2 except that a string is printed before each table which says what is listed below it.

X = TRand(5)

-- table of 5 random values between 0 and 1

Y = TRand(5,10,20)

-- table of 5 random values between 10 and 20

List( "\nX data", X, "\nY data", Y )

-- also print a string before each table.

The result from List() looks as follows.

X data

0.1171

0.747124

0.616108

0.921049

0.203772

 

Y data

14.6348

17.5863

19.5666

16.9369

12.4473

Related Topics

Contents

Variable Argument Functions

TList

Printf