Module util

Random utility functions

Functions

in_table (needle, haystack) Checks if "needle" is in "haystack", and returns the key (and value) if so
tweak (val) Returns a number between -10% and +10% of the original number.
round (val) Rounds a number up if >= .5, down if <.5
calc_distance (fromX, fromY, toY) Uses the Pythagorean Theorem to calculate the distance between two points.
calc_distance_squared (fromX, fromY, toY) Adds the squared X and Y distances between points.
ucfirst (string) Convert the first letter of a string to uppercase.
ucfirstall (s) Convert the first letter of every word in a string to uppercase.
lcfirst (string) Convert the first letter of a string to lowercase.
lcfirstall (s) Convert the first letter of every word in a string to lowercase.
explode (string, delim, Table.) "Explode" a string into an array
vowel (string) Returns true if the string starts with a vowel, false otherwise
get_random_element (t) Gets a random element from a table.
get_random_key (t) Gets a random key from a table.
get_largest (t) Gets the largest numerical value from a table
copy_table (t) Copies a table deeply (copies any sub-tables as well).
shuffle (t) Shuffles the order of table Also useful at turning an associative table into a numbered array Note: Only use if you want the resulting table to have sequentially numbered values!
count (t) Counts the number of entries in a table (even if not sequentially numbered)
merge_tables (Any) Combines multiple tables into one and returns the result.
sort_table (t, key) Sorts a table
print_table (t) Prints the keys and values in a table
get_unit_vector (fromX, fromY, toY) Determines what directions a set of coordinates is in from an origin point.
calc_angle (fromX, fromY, toY) Calculate the angle between two points


Functions

in_table (needle, haystack)
Checks if "needle" is in "haystack", and returns the key (and value) if so

Parameters:

  • needle Anything. The item to look for.
  • haystack Table. The table to search through.

Returns:

  1. Anything or False. The index of the value being searched for. If it wasn't found, then FALSE.
  2. Anything or nil. The value being searched for. If it wasn't found, then nil.
tweak (val)
Returns a number between -10% and +10% of the original number.

Parameters:

  • val Number. The number to tweak.

Returns:

    Number. The tweaked number.
round (val)
Rounds a number up if >= .5, down if <.5

Parameters:

  • val number to round.

Returns:

    Number. The rounded number.
calc_distance (fromX, fromY, toY)
Uses the Pythagorean Theorem to calculate the distance between two points.

Parameters:

  • fromX Number. The origin X-coordinate.
  • fromY Number. The origin Y-coordinate.
  • toY Number. The destination Y-coordinate.

Returns:

    Number. The distance.
calc_distance_squared (fromX, fromY, toY)
Adds the squared X and Y distances between points. Use this if you're just comparing distances to see which is farther rather than needing to know what the distances actually are, since this is faster.

Parameters:

  • fromX Number. The origin X-coordinate.
  • fromY Number. The origin Y-coordinate.
  • toY Number. The destination Y-coordinate.

Returns:

    Number. The sum of the squared X and Y distances between the points.
ucfirst (string)
Convert the first letter of a string to uppercase.

Parameters:

  • string String. The string to process.

Returns:

    String. The original string, but with the first letter uppercase.
ucfirstall (s)
Convert the first letter of every word in a string to uppercase.

Parameters:

  • s String. The string to process.

Returns:

    String. The original string, but with the first letter of every word uppercase.
lcfirst (string)
Convert the first letter of a string to lowercase.

Parameters:

  • string String. The string to process.

Returns:

    String. The original string, but with the first letter lowercase.
lcfirstall (s)
Convert the first letter of every word in a string to lowercase.

Parameters:

  • s String. The string to process.

Returns:

    String. The original string, but with the first letter of every word lowercase.
explode (string, delim, Table.)
"Explode" a string into an array

Parameters:

  • string String. The string to turn into an array.
  • delim String. The string used to delineate where each entry starts.
  • Table. A table of strings.
vowel (string)
Returns true if the string starts with a vowel, false otherwise

Parameters:

  • string String.

Returns:

    Boolean. Whether the string starts with a vowel or not.
get_random_element (t)
Gets a random element from a table. NOTE: This probably will not work properly if a table has both numbered AND associative keys.

Parameters:

  • t Table. The table to get an element from.

Returns:

    Anything. A random element from the table.
get_random_key (t)
Gets a random key from a table. NOTE: This probably will not work properly if a table has both numbered AND associative keys.

Parameters:

  • t Table. The table to get an element from.

Returns:

    Anything. A random keyfrom the table.
get_largest (t)
Gets the largest numerical value from a table

Parameters:

  • t Table. The table to look through.

Returns:

  1. Number. The largest number.
  2. Anything. The key of the largest number.
copy_table (t)
Copies a table deeply (copies any sub-tables as well). WARNING: This will probably hang if your table contains a table that contains the original table.

Parameters:

  • t Table. The table to copy.

Returns:

    Table. A copy of the original table.
shuffle (t)
Shuffles the order of table Also useful at turning an associative table into a numbered array Note: Only use if you want the resulting table to have sequentially numbered values!

Parameters:

  • t Table. The table to shuffle.

Returns:

    Table. A new version of the shuffled table.
count (t)
Counts the number of entries in a table (even if not sequentially numbered)

Parameters:

  • t Table. The table to count.

Returns:

    Number. The number of entries in the table, or 0 if it's not a table.
merge_tables (Any)
Combines multiple tables into one and returns the result. Does not respect the values of the keys - the resulting table will have sequentially-numbered keys.

Parameters:

  • Any number of tables.

Returns:

    Table. A new table with all the other tables merged together.
sort_table (t, key)
Sorts a table

Parameters:

  • t Table. The table to sort
  • key Anything. The key to use to compare, if the table t contains subtables. Optional, if blank compares the values directly
print_table (t)
Prints the keys and values in a table

Parameters:

  • t Table. The table to print
get_unit_vector (fromX, fromY, toY)
Determines what directions a set of coordinates is in from an origin point. Doesn't actually have anything to do with unit vectors.

Parameters:

  • fromX Number. The origin X-coordinate.
  • fromY Number. The origin Y-coordinate.
  • toY Number. The destination Y-coordinate.

Returns:

  1. Number. -1 if toX is to the left of fromX, +1 if it's to the right, 0 if they're the same.
  2. Number. -1 if toY is up from fromY, +1 if it's down, 0 if they're the same.
calc_angle (fromX, fromY, toY)
Calculate the angle between two points

Parameters:

  • fromX Number. The origin X-coordinate.
  • fromY Number. The origin Y-coordinate.
  • toY Number. The destination Y-coordinate.

Returns:

    Number. The angle
generated by LDoc 1.5.0 Last updated 2025-05-24 09:49:55