Search for an item in a Lua list

后端 未结 10 2414
青春惊慌失措
青春惊慌失措 2020-11-29 00:56

If I have a list of items like this:

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

how do I check if \"orange\" is in this

10条回答
  •  一向
    一向 (楼主)
    2020-11-29 01:22

    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.

提交回复
热议问题