Skip to content
everloot

Lore archive · Warcraft Wiki, 2023 dump

Table Helpers

Last wiki revision 2012-07-20

Miscellaneous table helper functions.

tinsertbeforeval(tab, valBefore, val) boolRemoved = tremovebyval(tab, val) num = tcount(tab) tcopy(to, from)

Note that tinsertbeforeval and tremovebyval iterate over the table to find their targets; they can be quite slow compared to regular inserts/deletes.

local tab = { "alice", "charles" } tinsertbeforeval(tab, "charles", "bob"); → tab: "alice", "bob", "charles" tinsertbeforeval(tab, "alice", "zeta"); → tab: "zeta", "alice", "bob", "charles"

bool = tremovebyval(tab, "zeta"); → bool = true bool = tremovebyval(tab, "zeta"); → bool = false

tab2["foo"]="bar"; tab2["abc"]="xyz"; myCount = tcount(tab2); → myCount = 2

tcopy(tab2, tab); → tab2: ["foo"]="bar", ["abc"]="xyz", [1]="alice", [2]="bob", [3]="charles"

-- tinsertbeforeval: insert a value before another value (given only values) -- assumes integer-indexed table, or "before" loses its meaning function <PREFIX>_tinsertbeforeval(tab, valBefore, val) for k,v in ipairs(tab) do if(v==valBefore) then table.insert(tab, k, val); return; end end -- print("insertbeforeval: Could not find \""..valBefore.."\". Appending last."); table.insert(tab, val); end -- tremovebyval: remove a table row given its value function <PREFIX>_tremovebyval(tab, val) for k,v in pairs(tab) do if(v==val) then table.remove(tab, k); return true; end end return false; end -- tcount: count table members even if they're not indexed by numbers function <PREFIX>_tcount(tab) local n = 0 for _ in pairs(tab) do n = n + 1 end return n end -- tcopy: recursively copy contents of one table to another function <PREFIX>_tcopy(to, from) -- "to" must be a table (possibly empty) for k,v in pairs(from) do if(type(v)=="table") then to[k] = {} <PREFIX>_tcopy(to[k], v); else to[k] = v; end end end

Categories

  • User defined functions

Source

https://warcraft.wiki.gg/wiki/Table_Helpers · retrieved 2023-11-13 · CC BY-SA 4.0

Converted from wikitext with templates, tables and images removed, so some sentences may be missing values.

This archive predates WoW: Forever. For collected gameplay facts see the Forever database. Search the archive.