Quantcast WoWInterface - View Single Post - Tables again...
Thread: Tables again...
View Single Post
12-20-19, 11:38 AM   #7
Vrul
A Rage Talon Dragon Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 344
Think of Lua tables as maps that can also be treated as arrays. The array behavior is only for the portion of the map that starts with the key of integer 1 and continues until a nil value is reached for sequential integer keys.

ipairs, #table, and the functions found in table (such as table.insert) only apply to the array portion of the map.

Think of #table as:
Code:
local function GetArraySize(table)
    local index = 1
    while table[index] ~= nil do
        index = index + 1
    end
    return index - 1
end
Think of table.insert as:
Code:
local function InsertIntoArray(table, index, value)
    for i = #table, index, - 1 do
        table[i + 1] = table[i]
    end
    table[index] = value
end
Think of table.remove as:
Code:
local function RemoveFromArray(table, index)
    local value = table[index]
    for i = index, #table - 1 do
        table[i] = table[i + 1]
    end
    table[#table] = nil
    return value
end
  Reply With Quote