Section Tables
tables utility functions.
Summary
| Return type | Function and summary |
|---|---|
| ArrayClear(table t) Optimization of clearing a table. | |
| boolean | ArrayContains(table tab, term) Does tab contains term? |
| table, int, int, int | CompareTables(table lines1, table lines2) Compare two tables, returns a diff-like format. |
| boolean | IN(term, ...) Check if term is in the list of following arguments. |
| boolean | assertInArray(value, table t, string text, boolean visual) Assert a value is found in a table. |
| boolean | isTable(obj) Returns true if obj is a table or a MyrScript object represented as a table such as Score or Target. |
| longest_common_subsequence(a, b) Longest Common Subsequence (LCS) algorithm, basis for data comparison. | |
| string | string_join(table list, string delimiter) Concat the elements of table list, separated by the string delimiter. |
| table | string_split(string s, string delim, int limit) Split the string s by a string delimiter delim. |
| table | string_split_lines(string s) Split the string s by lines, handle \r, \n and \r\n. |
| table_insert_multi(table t, ...) Insert multiple items in a table in one call. | |
| tdebug(...) Return the structure of a table, Collection or Map for debugging to the console. |
Functions
assertInArray(value, table t, string text, boolean visual)
Assert a value is found in a table.
| Parameter | Type | Default | Description |
|---|---|---|---|
| value | number or string. nil always fails the assertion. | ||
| t | table | List of values in which value is searched for. | |
| text | string | Text to print on console and throw if expr ~= expected | |
| visual | boolean | false | Visual alert. Does not stop the script with a console error message, returns true if assertion is correct, nil if it fails. Your script must handle the returned value to stop gracefully. |
- Return
- boolean:
trueif assertion is correct,nil(for false) if it failed. If visual ~= true, no need to check the returned value, script will die with a console error. - Error
- If value not found in t
IN(term, ...)
Check if term is in the list of following arguments.
| Parameter | Type | Default | Description |
|---|---|---|---|
| term | string, number or objects that support == comparison | ||
| ... | List of arguments, of undefined length. If one (or more) arguments are table, Collection or Map then term is searched in their items. Note that Collection and Map values must have the same type than type(term). |
- Return
- boolean:
trueif the list contains term,nil(for false) if the list doesn't contains term - See
- ArrayContains
isTable(obj)
Returns true if obj is a table or a MyrScript object represented as a table such as Score or Target.
| Parameter | Type | Default | Description |
|---|---|---|---|
| obj | obj |
- Return
- boolean: boolean
ArrayClear(table t)
Optimization of clearing a table.
tremove is time consuming because it shift all items after the removed one. tremove from last to first would be a bit faster.
but setting items to nil is even faster.
| Parameter | Type | Default | Description |
|---|---|---|---|
| t | table | The table to clear. |
ArrayContains(table tab, term)
Does tab contains term?
| Parameter | Type | Default | Description |
|---|---|---|---|
| tab | table | A table of numbers or strings | |
| term | A number or a string to find in tab |
- Return
- boolean:
true, ornil(for false) - Error
- if tab's items are not comparable with term
tdebug(...)
Return the structure of a table, Collection or Map for debugging to the console.
| Parameter | Type | Default | Description |
|---|---|---|---|
| ... | Structure(s) to debug |
table_insert_multi(table t, ...)
Insert multiple items in a table in one call.
| Parameter | Type | Default | Description |
|---|---|---|---|
| t | table | t | |
| ... | Undefined number of arguments |
longest_common_subsequence(a, b)
Longest Common Subsequence (LCS) algorithm, basis for data comparison.
This compares 2 strings or tables (of numbers or strings) and returns a string or a table containing common chars or items.
For the two strings "ABC" and "ACE", the LCS is "AC".
| Parameter | Type | Default | Description |
|---|---|---|---|
| a | First string or table (of comparable elements) | ||
| b | Second string or table (of comparable elements) |
- Return
- A string if a and b are strings, else a table.
- Error
- if
type(a) ~= type(b) - See
- CompareTextFiles
- CompareTables
- CompareStrings
- https://en.wikipedia.org/wiki/Longest_common_subsequence
- https://www.geeksforgeeks.org/dsa/longest-common-subsequence-dp-4/
CompareTables(table lines1, table lines2)
Compare two tables, returns a diff-like format.
The diff format is used for data comparison, or, in development environnement, to compare old and new lines of source code.
tables in arguments must contain comparable (==) and tostring()able items: so it's restricted to numbers and strings. For complex datas, consider serialization.
| Parameter | Type | Default | Description |
|---|---|---|---|
| lines1 | table | First (oldest) table of strings | |
| lines2 | table | Second (newest) table of strings |
- Returns
- table: Lines in approximate diff format:
- Added lines start with a + (plus)
- Removed lines start with a - (minus)
- Common lines (unchanged) start with a space.
- int: Number of added lines
- int: Number of removed lines
- int: Number of unchanged lines
- See
- CompareTextFiles
- CompareStrings
string_split(string s, string delim, int limit)
Split the string s by a string delimiter delim.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | The string to split | |
| delim | string | The delimiter | |
| limit | int | 0 | Maximum number of slices of string, values <= 1 are ignored.. |
- Return
- table: An array of at least one item (s if delim is not found)
- Example
local t = string_split("Hello World!", " ") print(t[1]) --> Hello print(t[2]) --> World!
string_split_lines(string s)
Split the string s by lines, handle \r, \n and \r\n.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | The string to split |
- Return
- table: A table of at least one element (s if line separator is not found)
string_join(table list, string delimiter)
Concat the elements of table list, separated by the string delimiter.
| Parameter | Type | Default | Description |
|---|---|---|---|
| list | table | list | |
| delimiter | string | "" |
- Return
- string: string
- Example
string_join({"Anna", "Bob", "Charlie", "Dolores"}, ", ") --> Anna, Bob, Charlie, Dolores