flattenn


The flattenn function "flattens" a general lua table to a 1-dimensional array. This procedure discards all table keys and sub-tables but retains all values as an indexed 1-dimensional array. Flattening is useful, for example, to access all table data without keys and without keeping track of accessing sub-tables. Use the flatten function to retain only the numeric values from a table.

Syntax

new_array = flattenn( tbl )

bullet.gif    tbl is a general lua table.

bullet.gif    new_array is the flattened 1-dimensional array.

Examples

The following script flattens a complex, ugly table. The oiginal and flattened tables are listed below the script.

t = { 6, -2, 15, "a", {5,"b"}, c="5.4" }

 

list(t)

-- list the original table

tnew = flattenn(t)

-- flattenn to 1-dimension

list(tnew)

-- list the result

In the listing below, notice that all values are retained but the keys and sub-tables are removed.

 

Original Table

Flattened Table

{

  [1]=6

  [2]=-2

  [3]=15

  [4]="a"

  [5]=

  {

    [1]=5

    [2]="b"

  }

  [c]="5.4"

}

{

  [1]=6

  [2]=-2

  [3]=15

  [4]="a"

  [5]=5

  [6]="b"

  [7]="5.4"

}

Related Topics

Table and Array Functions

flatten


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