WoW:API tContains: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(Missing end; fixed a syntax error.)
No edit summary
 
(One intermediate revision by one other user not shown)
Line 6: Line 6:




<big>'''Implementation'''</big>
; Implementation
function tContains(table, item)
<lua>
        local index = 1;
function tContains(table, item)
    local index = 1
         while table[index] do
         while table[index] do
                if ( item == table[index] ) then
            if item == table[index] then
                        return 1;
                return 1
                end
            end
                index = index + 1;
            index = index + 1
         end
         end
        return nil;
    return nil
end
end
</lua>


 
== Examples ==
== Example ==


  banana = { "yellow", "curved", "yummy" };
  banana = { "yellow", "curved", "yummy" };
Line 26: Line 27:
  eatme = tContains( lotus, "yummy" );
  eatme = tContains( lotus, "yummy" );


<big>'''Result'''</big>
;Result
  eatme = false
  eatme = false

Latest revision as of 21:17, 2 August 2025

WoW Lua


tContains(table, value)

Returns true (1) if value is in table, false (nil) otherwise. Note: this is not a standard Lua function, but was added by Blizzard to simplify table searches.


Implementation
function tContains(table, item)
    local index = 1
        while table[index] do
            if item == table[index] then
                return 1
            end
            index = index + 1
        end
    return nil
end

Examples

banana = { "yellow", "curved", "yummy" };
lotus = { "pink", "pretty" };
eatme = tContains( lotus, "yummy" );
Result
eatme = false