ismultidim


The ismultidim function returns true if a table contains any sub-tables, indicating that it is a "multi-dimensional" table. A 1-dimensional table returns false. This test may be useful for determining which functions to apply to a given table.

Syntax

bIsTrue = ismultidim( tbl )

bullet.gif    tbl is a general lua table.

bullet.gif    bIsTrue is returned as boolean true of the table is multi-dimensional.

Examples

The following script tests whether a table has dimensionality greater than 1. The table t contains two sub-tables, and one sub-table contains another sub-table, hence the result is true. The booltostr function is used to convert the boolean result to a string

t = { { 1, "c", a=-12, dar={17,"v"} }, {-15, 24, 3, 5} }

-- create a table

bIsTrue = ismultidim( t )

-- test the dimensionality

Printf("Multi = %s\n", booltostr(bIsTrue) )

-- Result: Multi = true

  

The next script creates a 2-dimensional array with no keys and no non-numeric values, Since it contains the sub-table col = {}, the test returns true:

row = {}

-- setup a table of rows

for j = 1, 4 do

-- for each row

  col = {}

-- create a column table

  for i = 1,3 do

-- for each column

    col[i] = i*j

-- assign the value to the column

  end

 

  row[j] = col

-- assign the column table to the row

end

 

bIsTrue = ismultidim( row )

-- test the dimensionality

Printf("Multi = %s\n", booltostr(bIsTrue) )

-- Result: Multi = true

The following table contains no sub-tables. Even though it contains a key and is therefore not an "array", it is not multi-dimensional, so the test returns false.

t = { -15, 24, p=3, 5, "a" }

-- create a 1-dimensional table

bIsTrue = ismultidim( t )

-- test the dimensionality

Printf("Multi = %s\n", booltostr(bIsTrue) )

-- Result: Multi = false

Related Topics

Table and Array Functions, booltostr


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2024 Mirametrics, Inc. All Rights Reserved.