How to check if a value is NOT anywhere in a Lua table?

Adam

I am pretty new to lua so please excuse me if this question is too basic. I was wondering if there is a good way to check if a value is not in a lua table. Something like:

if 5 ~= t[1] or 5 ~= t[2] or 5 ~= t[3] ... then end

but less stupid.

This

for i,v in ipairs(t) do
   if  5 ~= v then
   end
end

does not really work because I want to check if it does not show up anywhere in the table rather than if it equals to any given value.

Probably the only somewhat viable solution I could think of so far would be something like

check = 0
for i,v in ipairs(t) do
   if  5 == v then
   check = 1
   end
end

if check == 0 then end

but that still looks kind of cumbersome...

Thanks so much!

Adir Ratzon

If you need to check whether an item exists in array, you better keep a hash map to that array. This is obviously ineffective to go through the whole (or part of the) array for each check. I'll suggest you to create the mapping and only after, do your checks. Example:

local function array_map(array)
  local map = {}
  for _, item in ipairs(array) do
    map[item] = true
  end
  return map
end

local array = {1,2,3,4,5,6,7}
local arr_map = array_map(array)

if arr_map[1] then 
  print("The array has item 1")
end

if not arr_map[10] then
  print("Item 10 is not part of the array")
end

This is how you get your tests in constant time of ϑ(1) + n once for the map build.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I get first table value in Lua

分類Dev

lua how to get table within table use variable value rather than variable as an index? TTS

分類Dev

Print value from a lua table if pattern matches

分類Dev

How to zscan over lua table results in script

分類Dev

How to check for a value in an array

分類Dev

Lua)) how to loop table of table and get a specific property?

分類Dev

How to check that every value in a hash-table satisfies a predicate in Common Lisp

分類Dev

Angular Material table checkbox row value on check

分類Dev

How to check if the contents of an array are in a table?

分類Dev

how to check if a word appears as a whole word in a string in Lua

分類Dev

Lua: How to assigned a return value from a function to a local value of a different function in lua

分類Dev

How to check if a value is present in a list?

分類Dev

how to check if a value exists in a dataframe

分類Dev

How to check if XML value exist?

分類Dev

How to check if value is logically integer?

分類Dev

How to check duplicate value in MySQL

分類Dev

How to check whether an item exists in the dynamodb table?

分類Dev

How to check if Redshift user can alter table

分類Dev

how to check wordpress custom table is empty or not

分類Dev

Create a generic PreparedStatement method to check if value does exist in a Table

分類Dev

SQL - check if a value in a list does not exist in table and return boolean accordingly

分類Dev

Lua map value as constructor

分類Dev

How to check if value is present in list in java

分類Dev

How to check for the presence of no value in a "params[:attribute]"

分類Dev

How to check if a value/property exist in JSON data

分類Dev

How to check radio button with radio button value

分類Dev

How to check if a value/property exist in JSON data

分類Dev

How to check if the value from the json Data is null

分類Dev

Read only iterable table in lua?

分類Dev

Lua - Size of table returning different

Related 関連記事

  1. 1

    How do I get first table value in Lua

  2. 2

    lua how to get table within table use variable value rather than variable as an index? TTS

  3. 3

    Print value from a lua table if pattern matches

  4. 4

    How to zscan over lua table results in script

  5. 5

    How to check for a value in an array

  6. 6

    Lua)) how to loop table of table and get a specific property?

  7. 7

    How to check that every value in a hash-table satisfies a predicate in Common Lisp

  8. 8

    Angular Material table checkbox row value on check

  9. 9

    How to check if the contents of an array are in a table?

  10. 10

    how to check if a word appears as a whole word in a string in Lua

  11. 11

    Lua: How to assigned a return value from a function to a local value of a different function in lua

  12. 12

    How to check if a value is present in a list?

  13. 13

    how to check if a value exists in a dataframe

  14. 14

    How to check if XML value exist?

  15. 15

    How to check if value is logically integer?

  16. 16

    How to check duplicate value in MySQL

  17. 17

    How to check whether an item exists in the dynamodb table?

  18. 18

    How to check if Redshift user can alter table

  19. 19

    how to check wordpress custom table is empty or not

  20. 20

    Create a generic PreparedStatement method to check if value does exist in a Table

  21. 21

    SQL - check if a value in a list does not exist in table and return boolean accordingly

  22. 22

    Lua map value as constructor

  23. 23

    How to check if value is present in list in java

  24. 24

    How to check for the presence of no value in a "params[:attribute]"

  25. 25

    How to check if a value/property exist in JSON data

  26. 26

    How to check radio button with radio button value

  27. 27

    How to check if a value/property exist in JSON data

  28. 28

    How to check if the value from the json Data is null

  29. 29

    Read only iterable table in lua?

  30. 30

    Lua - Size of table returning different

ホットタグ

アーカイブ