flatten
The flatten function "flattens" the numeric values of a general lua table to an indexed 1-dimensional array. This procedure discards all non-numeric values and table keys, and reduces all higher dimension tables to the base 1-dimensional array. Flattening is useful, for example, when a function that supports only 1-dimensional arrays, such as meansdev, need to run on values from a complex table. Use the flattenn when a multi-dimensional table must retain all keys and values when flattened to 1-dimensional form.
new_array = flatten( tbl )
tbl is a general lua table.
new_array is the flattened 1-dimensional array.
The following script creates a 3-D array with 5 planes, 4 rows, and 3 columns and flattens it to a 1-dimensional array.
|
-- create the master table |
|
-- 5 planes |
|
|
|
-- 4 rows |
|
|
|
-- 3 columns |
|
|
|
|
|
-- row[j] is the column aray |
|
|
|
-- plane[k] is the row array |
|
|
|
-- list the 3-dimensional array |
|
-- flatten 3-D to 1-D |
|
-- list the 1-dimensional array |
The next script flattens a complex, ugly table. The original and flattened tables are listed below the script.
|
|
|
-- list the original table |
|
-- flatten the table |
|
-- list the result |
Notice that all non-numeric values, keys, and sub-tables have been removed.
|
|
{ [1]=6 [2]=-2 [3]=15 [4]="a" [5]= { [1]=5 [2]="b" } [c]="5.4" } |
{ [1]=6 [2]=-2 [3]=15 [4]=5 } |
Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics,
Inc. All Rights Reserved.