HyperMath

Table Management

Table Management

Previous topic Next topic Expand/collapse all hidden text  

Table Management

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  
hmtoggle_plus1table.concate

table.concate: Concatenates two tables in a new table.
t = table.concate(t1, t2)
t, t1, t2: tables
 
Example
t1 = {1, 'A'}
t2 = {2, 'B'}
t = table.concate(t1, t2)

hmtoggle_plus1table.contains

table.contains: Checks if a value exists in table.
b = table.find(t, v)
b: a boolean (true, if it exists)
 
Example
t = {1, 'A', 2, 'B', true}
print(table.contains(t, 'A'))
print(table.contains(t, 'C'))

hmtoggle_plus1table.create

table.create: Creates a table using input arguments as values.

t = table.create(X1, X2, XN)
t: a table
X1 to XN: variables

Example

t = table.create(1,'a', nil, true, [1 2 4;5 5 6])

hmtoggle_plus1greytable.extract

table.extract: Extracts a subrange of a table by a index vector.
t2 = table.extract(t1, vectIdx)
with
t1, t2: a table
vectIdx: a vector with index to extract
 
Example
t1 = {1,[1 2 3],'a',{1},true}
t2 = table.extract(t1,[3, 5])
foreach(t2, print)

It creates a new table t2 with 'a' and true as elements.

table.extract is more convenient to use than table.subrange.  Here is the same example using table.subrange:

t1 = {1,[1 2 3],'a',{1},true}
tidx1 = table.subrange(t1, 3, 3)
tidx2 = table.subrange(t1, 5)
t2 = table.concate(tidx1 ,tidx2)
foreach(t2, print)

hmtoggle_plus1table.fieldnames

table.fieldnames: Returns a table of strings with the list of field names.
t = table.fieldnames(T)
t, T a table
 
Examples
T = {}
T.myfield1 = 'A'
T(1) = 'B'
T.myfield2 = 'C'
t = table.fieldnames(T)
for k = 1, Length(t) do print(t(k)) end

hmtoggle_plus1table.find

table.find: Returns indices of the value searched.
ind = table.find(t, v)
ind: column vector with index of position founded.
ind equals to empty matrix if the element is not found.
t: a table
v: value searched in table
 
Example
t = {1, 'A', 2, 'B', true}
print(table.find(t, 'A'))
t2= {1, 'A', 2, 'A', true}
print(table.find(t2, 'A'))

hmtoggle_plus1table.getfield

table.getfield: Returns value of the field in current table.
R = table.getfield(T,S
T a table
S a string: field name
R value
If the field does not exist, table.getfield returns an error.
 
Example
T = {}
T.myfield1 = 'A'
T(1) = 'B'
T.myfield2 = 'C'
print(table.getfield(T, 'myfield1'))
print(table.getfield(T, 'myfield2'))

hmtoggle_plus1table.getmethods

table.getmethods: Returns methods available in a table.
t = table.getmethods(t)
 
t,t1: tables
 
Example
t = table.getmethods(string)
for k = 1,Length(t) do print(t(k)) end
 
t = table.getmethods(table)
for k = 1,Length(t) do print(t(k)) end

hmtoggle_plus1table.isfield

table.isfield: Checks if the field exists in current table.
b = table.isfield(T, S)
T a table
S a string: field name
b true or false (a boolean)

 
Example
T = {}
T.myfield1 = 'A'
T(1) = 'B'
T.myfield2 = 'C'
print(table.isfield(T, 'myfield1'))
print(table.isfield(T, 'myfield3'))

hmtoggle_plus1table.isstring

table.isstring: Returns true if all elements of the table are string.
a empty table {} returns true
b = table.isstring(t)
t: table
b: true or false (boolean)
 
Example
T1 = {'A', 'B'}
T2 = {'1', 1}
T3 = {}
 
print(table.isstring(T1))
print(table.isstring(T2))
print(table.isstring(T3))

hmtoggle_plus1table.subrange

table.subrange: Get a sub range of a table.
t2 = table.subrange(t1, idx1)
t2 = table.subrange(t1, idx1, idx2)
t2: a table
idx1: first index
idx2: last index
 
Example
t = {1, 'A', 2 , 'B', 3, 'C', 4}
st1 = table.subrange(t, 3)
st2 = table.subrange(t, 3 , 4)