How to check if a table contains an element in Lua?

后端 未结 4 791
感情败类
感情败类 2020-12-04 11:36

Is there a method for checking if a table contains a value ? I have my own (naive) function, but I was wondering if something \"official\" exists for that ? Or something mor

4条回答
  •  被撕碎了的回忆
    2020-12-04 12:00

    Given your representation, your function is as efficient as can be done. Of course, as noted by others (and as practiced in languages older than Lua), the solution to your real problem is to change representation. When you have tables and you want sets, you turn tables into sets by using the set element as the key and true as the value. +1 to interjay.

提交回复
热议问题