Search for an item in a Lua list
Asked Answered
E

12

72

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?

Eckman answered 17/3, 2009 at 21:56 Comment(0)
P
94

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
Pediment answered 17/3, 2009 at 22:8 Comment(0)
I
38

Use the following representation instead:

local items = { apple=true, orange=true, pear=true, banana=true }
if items.apple then
    ...
end
Interferometer answered 17/3, 2009 at 22:19 Comment(4)
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.Pryce
This feels so much more elegant. Just used it to create a table that looked like {thingIAmLookingFor:true, secondThingIAmLookingFor:true}Hushaby
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?Novgorod
@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.Horrorstruck
P
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
Posner answered 20/3, 2009 at 0:30 Comment(2)
Does this still hold? Just a recommendation: more descriptive variable names would be helpful to many viewers.Moorefield
f for function, l for list, v for valuePosner
O
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

Orndorff answered 3/11, 2020 at 15:10 Comment(0)
O
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.

Oversee answered 17/3, 2009 at 22:13 Comment(0)
L
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"})

Ly answered 20/10, 2016 at 8:31 Comment(0)
C
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.

Chlores answered 11/4, 2014 at 14:42 Comment(0)
S
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
Sequestration answered 29/3, 2013 at 15:7 Comment(1)
How is this different from local workingtable={} workingtable[123] = true workingtable[456] = true if workingtable[456] then ... endLy
A
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.

Antoinette answered 31/8, 2021 at 7:35 Comment(0)
M
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
Megagamete answered 24/7, 2020 at 20:49 Comment(1)
Although this code might solve the problem, a good answer should also explain what the code does and how it helps.Schmeltzer
A
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
Ademption answered 27/10, 2020 at 17:31 Comment(0)
A
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
Aube answered 1/5, 2021 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.