Extensions of the table library and the shorthand helpers that go with it. Adds functional helpers (table.map, table.select, table.reduce, table.partition and others), filtering with table.keep_if and table.delete_if, comparison, conversion between hashes and arrays, and serialization through SFS with a fallback to JSON. a turns a table into an array that has the table library as its methods, w and wk build arrays and hashes out of words, and print_table replaces PrintTable. table.Merge is replaced with a version that leaves the class field of objects alone.

Summary

Functions

Removes the entries for which the callback returns true from the table itself.
Returns a copy of the table without the entries for which the callback returns true.
Converts a string back into a table.
Checks whether two tables have the same contents.
Returns the first element of an array.
Flattens the tables nested inside of a table into a single array.
hash(tab)shared
Returns the memory address of a table, which identifies it uniquely while it exists.
Concatenates all values of a table into a single string, including those of nested tables.
Removes the entries for which the callback does not return true from the table itself.
Returns a copy of the table that only has the entries for which the callback returns true.
last(tab)shared
Returns the last element of an array.
map(t, c)shared
Creates an array out of the values a callback returns for every value of a table.
Creates an array out of the values a callback returns for every key-value pair of a table.
Recursively merges the source table into the destination table, overwriting existing keys.
Checks whether the callback returns a truthy value for none of the entries of the table.
Checks whether the callback returns a truthy value for exactly one entry of the table.
Splits the values of a table into two arrays: those for which the callback returns a truthy value and all the others.
Creates an array of consecutive integers.
Combines all elements of an array into a single value, by applying an operator to the running result and each element in order.
Removes all functions from a table and from the tables nested in it.
Merges a table into another one like table.Merge, but leaves __index of both tables alone and skips the class key and any field of from that refers to from itself.
Filters the values of a table with a callback, or collects a single field from every table inside of it.
Converts a table into the string format.
Returns the part of an array between two indexes, both inclusive.
sum(tab)shared
Adds up all elements of an array.
Converts a hash into an array of { key, value } pairs.
Converts an array of { key, value } pairs into a hash.
Returns a copy of the table without duplicate values.

Functions

table.delete(tab, callback)

shared lib/stdlib/sh_table.lua:355

Removes the entries for which the callback returns true from the table itself.

Parameters

  • tab Map/List table to modify
  • callback Function callback(key, value), returns true to remove the entry

Returns

  • Map/List the same table

See also

table.delete_if(tab, callback)

shared lib/stdlib/sh_table.lua:338

Returns a copy of the table without the entries for which the callback returns true. The entries keep their keys and the original table is not modified.

Parameters

  • tab Map/List
  • callback Function callback(key, value), returns true to leave the entry out

Returns

  • Map/List the filtered copy

See also

table.deserialize(data)

shared lib/stdlib/sh_table.lua:536

Converts a string back into a table. Uses SFS at first, if it fails it falls back to JSON.

Parameters

  • data String string to convert

Returns

  • Map decoded table; an empty table if data is not a string, nil if it is neither valid SFS nor valid JSON

table.equal(tab1, tab2)

shared lib/stdlib/sh_table.lua:277

Checks whether two tables have the same contents. Nested tables are compared recursively.

Parameters

  • tab1 Map/List
  • tab2 Map/List

Returns

  • Boolean true if the contents are equal, false if not or if either one is not a table

table.first(tab)

shared lib/stdlib/sh_table.lua:401

Returns the first element of an array.

Parameters

  • tab List

Returns

  • Any the first element, or nil if the array is empty

table.flatten(tab)

shared lib/stdlib/sh_table.lua:222

Flattens the tables nested inside of a table into a single array. Tables that have a class field (objects) are treated as values.

Parameters

  • tab Map/List

Returns

  • List the flattened values

table.hash(tab)

shared lib/stdlib/sh_table.lua:308

Returns the memory address of a table, which identifies it uniquely while it exists.

Parameters

  • tab Map/List

Returns

  • String the address without the 'table: 0x' prefix
  • Number 1 if the prefix was removed

table.join(tab, sep)

shared lib/stdlib/sh_table.lua:316

Concatenates all values of a table into a single string, including those of nested tables.

Parameters

  • tab Map/List
  • sep String optional, defaults to '' separator to put between the values

Returns

  • String the concatenated values

table.keep(tab, callback)

shared lib/stdlib/sh_table.lua:388

Removes the entries for which the callback does not return true from the table itself.

Parameters

  • tab Map/List table to modify
  • callback Function callback(key, value), returns true to keep the entry

Returns

  • Map/List the same table

See also

table.keep_if(tab, callback)

shared lib/stdlib/sh_table.lua:371

Returns a copy of the table that only has the entries for which the callback returns true. The entries keep their keys and the original table is not modified.

Parameters

  • tab Map/List
  • callback Function callback(key, value), returns true to keep the entry

Returns

  • Map/List the filtered copy

See also

table.last(tab)

shared lib/stdlib/sh_table.lua:408

Returns the last element of an array.

Parameters

  • tab List

Returns

  • Any the last element, or nil if the array is empty

table.map(t, c)

shared lib/stdlib/sh_table.lua:74

Creates an array out of the values a callback returns for every value of a table. Values for which the callback returns nil are left out.

local names = table.map(player.GetAll(), function(v) return v:name() end)

Parameters

  • t Map/List table to go through
  • c Function callback(value), returns the value to store or nil to skip it

Returns

  • List values returned by the callback

See also

table.map_kv(t, c)

shared lib/stdlib/sh_table.lua:96

Creates an array out of the values a callback returns for every key-value pair of a table. Pairs for which the callback returns nil are left out.

local lines = table.map_kv({ a = 1, b = 2 }, function(k, v) return k..' = '..v end)

Parameters

  • t Map/List table to go through
  • c Function callback(key, value), returns the value to store or nil to skip it

Returns

  • List values returned by the callback

table.Merge(dest, source)

shared lib/stdlib/sh_table.lua:22

Recursively merges the source table into the destination table, overwriting existing keys. Replaces the built-in table.Merge. Unlike the built-in, a table stored under the class key is assigned by reference instead of being merged.

Parameters

  • dest Map table to merge into, modified in place
  • source Map table to take the values from

Returns

  • Map dest

table.none(tab, callback)

shared lib/stdlib/sh_table.lua:436

Checks whether the callback returns a truthy value for none of the entries of the table.

Parameters

  • tab Map/List
  • callback Function callback(key, value)

Returns

  • Boolean

table.one(tab, callback)

shared lib/stdlib/sh_table.lua:416

Checks whether the callback returns a truthy value for exactly one entry of the table.

Parameters

  • tab Map/List
  • callback Function callback(key, value)

Returns

  • Boolean

table.partition(tab, callback)

shared lib/stdlib/sh_table.lua:454

Splits the values of a table into two arrays: those for which the callback returns a truthy value and all the others.

local admins, others = table.partition(player.GetAll(), function(k, v) return v:IsAdmin() end)

Parameters

  • tab Map/List
  • callback Function callback(key, value)

Returns

  • List values the callback accepted
  • List values it did not

table.range(from, to)

shared lib/stdlib/sh_table.lua:628

Creates an array of consecutive integers.

Parameters

  • from Number first number
  • to Number last number, inclusive

Returns

  • List<Number>

table.reduce(tab, op)

shared lib/stdlib/sh_table.lua:196

Combines all elements of an array into a single value, by applying an operator to the running result and each element in order. The running result starts at 0.

table.reduce({ 1, 2, 3 }, '+') -- 6
table.reduce({ 1, 5, 3 }, function(result, v) return math.max(result, v) end) -- 5

Parameters

  • tab List
  • op String/Function one of '+', '-', '*', '/', '**', '^' and '%', or a callback(result, value) that returns the new result

Returns

  • Any the final result, a Number for the built-in operators

table.remove_functions(obj)

shared lib/stdlib/sh_table.lua:161

Removes all functions from a table and from the tables nested in it. Modifies the table.

Parameters

  • obj Any table to clean up, anything else is returned as-is

Returns

  • Any the same object

table.safe_merge(to, from)

shared lib/stdlib/sh_table.lua:39

Merges a table into another one like table.Merge, but leaves __index of both tables alone and skips the class key and any field of from that refers to from itself.

Parameters

  • to Map table to merge into, modified in place
  • from Map table to take the values from

Returns

  • Map to

table.select(t, what)

shared lib/stdlib/sh_table.lua:123

Filters the values of a table with a callback, or collects a single field from every table inside of it.

local alive = table.select(player.GetAll(), function(v, k) return v:Alive() end)
local names = table.select({ { name = 'a' }, { name = 'b' } }, 'name') -- { 'a', 'b' }

Variants

  • table.select(t, what)
    • t Map/List table to filter
    • what Function callback(value, key), the value is kept unless it returns false
  • table.select(t, what)
    • t Map/List table that consists of tables
    • what String/Number key to read from every value that is a table

Returns

  • List selected values

table.serialize(tab)

shared lib/stdlib/sh_table.lua:510

Converts a table into the string format.

Parameters

  • tab Map/List table to convert

Returns

  • String hex-encoded SFS data
  • JSON if SFS fails, or an empty string if both fail or tab is not a table

table.slice(t, from, to)

shared lib/stdlib/sh_table.lua:148

Returns the part of an array between two indexes, both inclusive.

Parameters

  • t List
  • from Number first index
  • to Number last index

Returns

  • List the elements between from and to

table.sum(tab)

shared lib/stdlib/sh_table.lua:214

Adds up all elements of an array.

Parameters

  • tab List<Number>

Returns

  • Number the sum, 0 for an empty array

table.to_array(tab)

shared lib/stdlib/sh_table.lua:475

Converts a hash into an array of { key, value } pairs.

table.to_array({ a = 1, b = 2 }) -- { { 'a', 1 }, { 'b', 2 } }, in no particular order

Parameters

  • tab Map

Returns

  • List<List> key-value pairs

See also

table.to_hash(tab)

shared lib/stdlib/sh_table.lua:492

Converts an array of { key, value } pairs into a hash.

table.to_hash({ { 'a', 1 }, { 'b', 2 } }) -- { a = 1, b = 2 }

Parameters

  • tab List<List> key-value pairs

Returns

  • Map

See also

table.uniq(tab, condition)

shared lib/stdlib/sh_table.lua:242

Returns a copy of the table without duplicate values. The values keep their original keys, so the copy of an array can have gaps in it.

Parameters

  • tab Map/List
  • condition Function optional, defaults to nil callback(value) that returns true for the values to leave out, used instead of the duplicate check

Returns

  • Map/List table of unique values

Defined in