Compact: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(Rephrased the purpose of the function to be a more straightforward explanation.) |
||
Line 2: | Line 2: | ||
<lowercasetitle></lowercasetitle> | <lowercasetitle></lowercasetitle> | ||
__NOTOC__ | __NOTOC__ | ||
This function | This function performs a search in a given table and returns a new table containing the values of specified variable name strings. | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> |
Latest revision as of 21:22, 4 March 2024
This function performs a search in a given table and returns a new table containing the values of specified variable name strings.
Syntax
table compact(table Array, table/string Variable)
Required Arguments
- Array: the table handles it recursively.
- Variable: table or string takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it.
Returns
Returns the output table with all the variables added to it.
Code
function compact(g, ...) local args = {...} local tbl = {} g = g or _G for i, v in ipairs(args) do for w in string.gmatch(v, "[%w_]+") do tbl[v] = g[w] end end return tbl end
Example
Click to collapse [-]
Shared Function-- AND FORMAT FUNCTION --- With EXEMPLE function format(s, tab) return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end)) end function compact(g, ...) local args = {...} local tbl = {} g = g or _G for i, v in ipairs(args) do for w in string.gmatch(v, "[%w_]+") do tbl[v] = g[w] end end return tbl end function testCompact() local tbl = { firstname = "Peter", lastname = "Griffin", age = 41 } return format("My lastname is ${lastname} and age its ${age}", compact(tbl, "lastname",'age')) end outputChatBox( testCompact() )
Author: @FroPop