Difference between revisions of "Manual:Table Functions"

From Mudlet
Jump to navigation Jump to search
m (moved Template:Table Functions to Manual:Table Functions: Manual:Basic Essentials)
Line 1: Line 1:
= Table Functions =
+
== Table Functions ==
  
==table.complement==
+
===table.complement===
 
; table.complement (set1, set2)
 
; table.complement (set1, set2)
 
: Returns a table that is the relative complement of the first table with respect to the second table. Returns a complement of key/value pairs.
 
: Returns a table that is the relative complement of the first table with respect to the second table. Returns a complement of key/value pairs.
Line 12: Line 12:
 
<lua></lua>
 
<lua></lua>
  
==table.concat==
+
===table.concat===
 
;table.concat(table, delimiter, startingindex, endingindex)
 
;table.concat(table, delimiter, startingindex, endingindex)
 
: Joins a table into a string. Each item must be something which can be transformed into a string.  
 
: Joins a table into a string. Each item must be something which can be transformed into a string.  
Line 48: Line 48:
 
</lua>
 
</lua>
  
==table.contains==
+
===table.contains===
 
; table.contains (t, value)
 
; table.contains (t, value)
 
: Determines if a table contains a value as a key or as a value (recursive).
 
: Determines if a table contains a value as a key or as a value (recursive).
Line 73: Line 73:
 
==table.getn==
 
==table.getn==
 
==table.intersection==
 
==table.intersection==
==table.insert==
+
===table.insert===
 
; table.insert(table, [pos,] value)
 
; table.insert(table, [pos,] value)
 
: Inserts element '''value''' at position '''pos''' in '''table''', shifting up other elements to open space, if necessary. The default '''value''' for '''pos''' is n+1, where n is the length of the table, so that a call table.insert(t,x) inserts x at the end of table t.
 
: Inserts element '''value''' at position '''pos''' in '''table''', shifting up other elements to open space, if necessary. The default '''value''' for '''pos''' is n+1, where n is the length of the table, so that a call table.insert(t,x) inserts x at the end of table t.
Line 88: Line 88:
 
{{note}} Inserting a function into a table is not good coding practice, and will not turn out how you think it would.
 
{{note}} Inserting a function into a table is not good coding practice, and will not turn out how you think it would.
  
==table.index_of==
+
===table.index_of===
==table.is_empty==
+
===table.is_empty===
 
; table.is_empty(table)
 
; table.is_empty(table)
 
: Check if a table is devoid of any values.
 
: Check if a table is devoid of any values.
Line 96: Line 96:
 
* '''table:'''
 
* '''table:'''
 
: The table you are checking for values.
 
: The table you are checking for values.
==table.load==
+
===table.load===
 
; table.load(location, table)
 
; table.load(location, table)
 
: Load a table from an external file into mudlet.
 
: Load a table from an external file into mudlet.
Line 113: Line 113:
 
-- You can load a table from anywhere on your computer, but it's preferable to have them consolidated somewhere connected to Mudlet.
 
-- You can load a table from anywhere on your computer, but it's preferable to have them consolidated somewhere connected to Mudlet.
 
</lua>
 
</lua>
==table.maxn==
+
===table.maxn===
 
;table.maxn(Table)
 
;table.maxn(Table)
 
:Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.)
 
:Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.)
  
==table.n_union==
+
===table.n_union===
==table.n_complement==
+
===table.n_complement===
==table.n_intersection==
+
===table.n_intersection===
==table.pickle==
+
===table.pickle===
==table.remove==
+
===table.remove===
 
; table.remove(table, value_position)
 
; table.remove(table, value_position)
 
: Remove a value from an indexed table, by the values position in the table.
 
: Remove a value from an indexed table, by the values position in the table.
Line 149: Line 149:
 
</lua>
 
</lua>
  
==table.save==
+
===table.save===
 
; table.save(location, table)
 
; table.save(location, table)
 
: Save a table into an external file in '''location'''.
 
: Save a table into an external file in '''location'''.
Line 165: Line 165:
 
table.save(getMudletHomeDir().."/mytable.lua", mytable)
 
table.save(getMudletHomeDir().."/mytable.lua", mytable)
 
</lua>
 
</lua>
==table.sort==
+
===table.sort===
 
;table.sort(Table [, comp])
 
;table.sort(Table [, comp])
 
:Sorts table elements in a given order, in-place, from <code>Table[1]</code> to <code>Table[n]</code>, where <code>n</code> is the length of the table.  
 
:Sorts table elements in a given order, in-place, from <code>Table[1]</code> to <code>Table[n]</code>, where <code>n</code> is the length of the table.  
Line 173: Line 173:
 
:The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.
 
:The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.
  
==table.size==
+
===table.size===
 
; table.size (t)
 
; table.size (t)
 
: Gets the actual size of non-index based tables.
 
: Gets the actual size of non-index based tables.
Line 194: Line 194:
 
</lua>
 
</lua>
  
==table.setn==
+
===table.setn===
==table.unpickle==
+
===table.unpickle===
==table.update==
+
===table.update===
==table.union==
+
===table.union===
 
[[Category:Mudlet Manual]]
 
[[Category:Mudlet Manual]]

Revision as of 01:54, 19 January 2012

Table Functions

table.complement

table.complement (set1, set2)
Returns a table that is the relative complement of the first table with respect to the second table. Returns a complement of key/value pairs.
Parameters
  • table1:
  • table2:
Example

<lua></lua>

table.concat

table.concat(table, delimiter, startingindex, endingindex)
Joins a table into a string. Each item must be something which can be transformed into a string.
Returns the joined string.
See also: string.split
Parameters
  • table:
The table to concatenate into a string. Passed as a table.
  • delimiter:
Optional string to use to separate each element in the joined string. Passed as a string.
  • startingindex:
Optional parameter to specify which index to begin the joining at. Passed as an integer.
  • endingindex:
Optional parameter to specify the last index to join. Passed as an integer.
Examples

<lua> --This shows a basic concat with none of the optional arguments testTable = {1,2,"hi","blah",} testString = table.concat(testTable) --testString would be equal to "12hiblah"

--This example shows the concat using the optional delimiter testString = table.concat(testTable, ", ") --testString would be equal to "1, 2, hi, blah"

--This example shows the concat using the delimiter and the optional starting index testString = table.concat(testTable, ", ", 2) --testString would be equal to "2, hi, blah"

--And finally, one which uses all of the arguments testString = table.concat(testTable, ", ", 2, 3) --testString would be equal to "2, hi" </lua>

table.contains

table.contains (t, value)
Determines if a table contains a value as a key or as a value (recursive).
Returns true or false
Parameters
  • t:
The table in which you are checking for the presence of the value.
  • value:
The value you are checking for within the table.
Example

<lua>local test_table = { "value1", "value2", "value3", "value4" } if table.contains(test_table, "value1") then

  echo("Got value 1!")

else

  echo("Don't have it. Sorry!")

end </lua> This example would always echo the first one, unless you remove value1 from the table.

table.foreachi

table.foreach

table.getn

table.intersection

table.insert

table.insert(table, [pos,] value)
Inserts element value at position pos in table, shifting up other elements to open space, if necessary. The default value for pos is n+1, where n is the length of the table, so that a call table.insert(t,x) inserts x at the end of table t.
See also: table.remove
Parameters
  • table:
The table in which you are inserting the value
  • pos:
Optional argument, determining where the value will be inserted.
  • value:
The variable that you are inserting into the table. Can be a regular variable, or even a table or function*.

Note Note: Inserting a function into a table is not good coding practice, and will not turn out how you think it would.

table.index_of

table.is_empty

table.is_empty(table)
Check if a table is devoid of any values.
Parameters
  • table:
The table you are checking for values.

table.load

table.load(location, table)
Load a table from an external file into mudlet.
See also: table.save
Parameters
  • location:
Where you are loading the table from. Can be anywhere on your computer.
  • table:
The table that you are loading.
Example:

<lua> -- This will load the table mytable from the lua file mytable present in your Mudlet Home Directory. table.load(getMudletHomeDir().."/mytable.lua", mytable) -- You can load a table from anywhere on your computer, but it's preferable to have them consolidated somewhere connected to Mudlet. </lua>

table.maxn

table.maxn(Table)
Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.)

table.n_union

table.n_complement

table.n_intersection

table.pickle

table.remove

table.remove(table, value_position)
Remove a value from an indexed table, by the values position in the table.
See also: table.insert
Parameters
  • table
The indexed table you are removing the value from.
  • value_position
The indexed number for the value you are removing.
Example

<lua> testTable = { "hi", "bye", "cry", "why" } table.remove(testTable, 1) -- will remove hi from the table -- new testTable after the remove testTable = { "bye", "cry", "why" } -- original position of hi was 1, after the remove, position 1 has become bye -- any values under the removed value are moved up, 5 becomes 4, 4 becomes 3, etc </lua> Note Note: To remove a value from a key-value table, it's best to simply change the value to nil. <lua> testTable = { test = "testing", go = "boom", frown = "glow" } table.remove(testTable, test) -- this will error testTable.test = nil -- won't error testTable["test"] = nil -- won't error </lua>

table.save

table.save(location, table)
Save a table into an external file in location.
See also: table.load
Parameters
  • location:
Where you want the table file to be saved. Can be anywhere on your computer.
  • table:
The table that you are saving to the file.
Example:

<lua> -- Saves the table mytable to the lua file mytable in your Mudlet Home Directory table.save(getMudletHomeDir().."/mytable.lua", mytable) </lua>

table.sort

table.sort(Table [, comp])
Sorts table elements in a given order, in-place, from Table[1] to Table[n], where n is the length of the table.
If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given, then the standard Lua operator < is used instead.
The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.

table.size

table.size (t)
Gets the actual size of non-index based tables.
Returns a number.
Parameters
  • t:
The table you are checking the size of.

Note Note: For index based tables you can get the size with the # operator: This is the standard Lua way of getting the size of index tables i.e. ipairs() type of tables with numerical indices. To get the size of tables that use user defined keys instead of automatic indices (pairs() type) you need to use the function table.size() referenced above. <lua> local test_table = { "value1", "value2", "value3", "value4" } myTableSize = #test_table -- This would return 4. local myTable = { 1 = "hello", "key2" = "bye", "key3" = "time to go" } table.size(myTable) -- This would return 3. </lua>

table.setn

table.unpickle

table.update

table.union