69

If I have a list of items like this:

local items = { "apple", "orange", "pear", "banana" }

how do I check if "orange" is in this list?

In Python I could do:

if "orange" in items:
    # do something

Is there an equivalent in Lua?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Jay
  • 1,423
  • 2
  • 11
  • 9

12 Answers12

92

You could use something like a set from Programming in Lua:

function Set (list)
  local set = {}
  for _, l in ipairs(list) do set[l] = true end
  return set
end

Then you could put your list in the Set and test for membership:

local items = Set { "apple", "orange", "pear", "banana" }

if items["orange"] then
  -- do something
end

Or you could iterate over the list directly:

local items = { "apple", "orange", "pear", "banana" }

for _,v in pairs(items) do
  if v == "orange" then
    -- do something
    break
  end
end
Panda
  • 6,955
  • 6
  • 40
  • 55
Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
36

Use the following representation instead:

local items = { apple=true, orange=true, pear=true, banana=true }
if items.apple then
    ...
end
Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
  • 3
    This is the best way to make a set (in the pure mathematical sense) of things in Lua. Bravo! However, since it has no concept of order, it doesn't necessarily answer the general question of "Search for an item in a Lua list?" if list order matters. – Mark Jun 21 '12 at 06:13
  • This feels so much more elegant. Just used it to create a table that looked like `{thingIAmLookingFor:true, secondThingIAmLookingFor:true}` – stiller_leser Nov 30 '15 at 18:53
  • 1
    This is a nice answer, but it doesn't address the question's general problem: what about an arbitrary list of strings? The equivalent `if "orange" in items` from Python doesn't require constructing your own specialized list. Is there a Lua way to take any list of strings and rebuild it in this manner? – Zim Aug 04 '18 at 17:14
  • @CalculatorFeline Some keys (having numbers, spaces, accents…) need the bracket notation. e.g.: `local items = { [42]=true, ['foo bar']=true, ['éà']=true }`. Actually the notation without brackets is a syntaxic sugar (as often in Lua). You may be interested by the [Tables Tutorial](http://lua-users.org/wiki/TablesTutorial). – Gras Double Aug 22 '18 at 11:07
21

You're seeing firsthand one of the cons of Lua having only one data structure---you have to roll your own. If you stick with Lua you will gradually accumulate a library of functions that manipulate tables in the way you like to do things. My library includes a list-to-set conversion and a higher-order list-searching function:

function table.set(t) -- set of list
  local u = { }
  for _, v in ipairs(t) do u[v] = true end
  return u
end

function table.find(f, l) -- find element v of l satisfying f(v)
  for _, v in ipairs(l) do
    if f(v) then
      return v
    end
  end
  return nil
end
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
4

Write it however you want, but it's faster to iterate directly over the list, than to generate pairs() or ipairs()

#! /usr/bin/env lua

local items = { 'apple', 'orange', 'pear', 'banana' }

local function locate( table, value )
    for i = 1, #table do
        if table[i] == value then print( value ..' found' ) return true end
    end
    print( value ..' not found' ) return false
end

locate( items, 'orange' )
locate( items, 'car' )

orange found
car not found

Doyousketch2
  • 2,060
  • 1
  • 11
  • 11
3

This is a swiss-armyknife function you can use:

function table.find(t, val, recursive, metatables, keys, returnBool)
    if (type(t) ~= "table") then
        return nil
    end

    local checked = {}
    local _findInTable
    local _checkValue
    _checkValue = function(v)
        if (not checked[v]) then
            if (v == val) then
                return v
            end
            if (recursive and type(v) == "table") then
                local r = _findInTable(v)
                if (r ~= nil) then
                    return r
                end
            end
            if (metatables) then
                local r = _checkValue(getmetatable(v))
                if (r ~= nil) then
                    return r
                end
            end
            checked[v] = true
        end
        return nil
    end
    _findInTable = function(t)
        for k,v in pairs(t) do
            local r = _checkValue(t, v)
            if (r ~= nil) then
                return r
            end
            if (keys) then
                r = _checkValue(t, k)
                if (r ~= nil) then
                    return r
                end
            end
        end
        return nil
    end

    local r = _findInTable(t)
    if (returnBool) then
        return r ~= nil
    end
    return r
end

You can use it to check if a value exists:

local myFruit = "apple"
if (table.find({"apple", "pear", "berry"}, myFruit)) then
    print(table.find({"apple", "pear", "berry"}, myFruit)) -- 1

You can use it to find the key:

local fruits = {
    apple = {color="red"},
    pear = {color="green"},
}
local myFruit = fruits.apple
local fruitName = table.find(fruits, myFruit)
print(fruitName) -- "apple"

I hope the recursive parameter speaks for itself.

The metatables parameter allows you to search metatables as well.

The keys parameter makes the function look for keys in the list. Of course that would be useless in Lua (you can just do fruits[key]) but together with recursive and metatables, it becomes handy.

The returnBool parameter is a safe-guard for when you have tables that have false as a key in a table (Yes that's possible: fruits = {false="apple"})

Luc Bloom
  • 1,120
  • 12
  • 18
3

Lua tables are more closely analogs of Python dictionaries rather than lists. The table you have create is essentially a 1-based indexed array of strings. Use any standard search algorithm to find out if a value is in the array. Another approach would be to store the values as table keys instead as shown in the set implementation of Jon Ericson's post.

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
2
function valid(data, array)
 local valid = {}
 for i = 1, #array do
  valid[array[i]] = true
 end
 if valid[data] then
  return false
 else
  return true
 end
end

Here's the function I use for checking if data is in an array.

KingofGamesYami
  • 345
  • 3
  • 12
1

Sort of solution using metatable...

local function preparetable(t)
 setmetatable(t,{__newindex=function(self,k,v) rawset(self,v,true) end})
end

local workingtable={}
preparetable(workingtable)
table.insert(workingtable,123)
table.insert(workingtable,456)

if workingtable[456] then
...
end
  • How is this different from ```local workingtable={} workingtable[123] = true workingtable[456] = true if workingtable[456] then ... end``` – Luc Bloom Oct 13 '17 at 07:12
1

The following representation can be used:

local items = {
    ["apple"]=true, ["orange"]=true, ["pear"]=true, ["banana"]=true
}

if items["apple"] then print("apple is a true value.") end
if not items["red"] then print("red is a false value.") end

Related output:

apple is a true value.
red is a false value.

You can also use the following code to check boolean validity:

local items = {
    ["apple"]=true, ["orange"]=true, ["pear"]=true, ["banana"]=true,
    ["red"]=false, ["blue"]=false, ["green"]=false
}

if items["yellow"] == nil then print("yellow is an inappropriate value.") end
if items["apple"] then print("apple is a true value.") end
if not items["red"] then print("red is a false value.") end

The output is:

yellow is an inappropriate value.
apple is a true value.
red is a false value.

Check Tables Tutorial for additional information.

ircama
  • 75
  • 1
  • 5
0
function table.find(t,value)
    if t and type(t)=="table" and value then
        for _, v in ipairs (t) do
            if v == value then
                return true;
            end
        end
        return false;
    end
    return false;
end
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
  • 1
    Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it helps. – BDL Jul 25 '20 at 10:28
0

you can use this solution:

items = { 'a', 'b' }
for k,v in pairs(items) do 
 if v == 'a' then 
  --do something
 else 
  --do something
 end
end

or

items = {'a', 'b'}
for k,v in pairs(items) do 
  while v do
    if v == 'a' then 
      return found
    else 
      break
    end
  end 
 end 
return nothing
0

A simple function can be used that :

  • returns nil, if the item is not found in table
  • returns index of item, if item is found in table
local items = { "apple", "orange", "pear", "banana" }

local function search_value (tbl, val)
    for i = 1, #tbl do
        if tbl[i] == val then
            return i
        end
    end
    return nil
end

print(search_value(items, "pear"))
print(search_value(items, "cherry"))

output of above code would be

3
nil
Rudra Lad
  • 31
  • 3