Find missing value in table or array

Hello,

Is there a way to iterate through a table or array and find missing value in it?
say i have t = {1,1,1,1,1}
and then in some point i remove one of those values and set it to 0

how can i find missing value? and then in some function set this 0 to 1

i read about it in some documents, but cant find it anywhere

any help is appreciated [import]uid: 16142 topic_id: 16232 reply_id: 316232[/import]

easy

local i  
  
t={1,1,1,0,1}  
  
for i=1,#t do  
 if t[i] == 0 then t[i]=1 end  
end  
  

however if you remove the element totally, by using NIL, not a 0 then that can cause issues. However for that you can use

local i  
local max\_elements = 100  
  
t={1,1,1,0,1}  
  
for i=1,max\_elements do  
 if t[i] == nil or t[i] == 0 then  
 t[i]=1   
 end  
end  

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16232 reply_id: 60441[/import]

wow, pretty cool, thanks for answer)

and a little question: what if there is not one but 2,3,4 zeroes in array, how can i set only the first found zero to 1? [import]uid: 16142 topic_id: 16232 reply_id: 60499[/import]

darkconsoles: just call ‘break’ once you have found it … that will exit the for loop :slight_smile: [import]uid: 52430 topic_id: 16232 reply_id: 60520[/import]

thanks for help, i really appreciate it
if you dont mind, here is another question:

say i have this table “t = {0,0}”
and i have two objects on screen and i need to touch them and they appear on another side of screen based on a value in table
in example, if t = {0,0} it will move in x = 50, and if t = {1,0} it will move in x = 100, and if t = {0,1} it will move to x=50

hope it make any sense) how can i do this? i dont think stacking all those “if” will do any good, there is gotta be another way [import]uid: 16142 topic_id: 16232 reply_id: 60584[/import]

so If I get this right,

0,0 = 50
1,0 = 100
0,1 = 50
1,1 = 100

so the second value means nothing in your example, it is the first value that is important.

Secondly, if you are trying to work with just these two values, you need to use simple Algebra

t = x + y
y = t - x

so, you need to use that way if you do not want to use “if”

try this, without using if statements,

local initial = 1

print( x ) --> should print 2 if Initial == 1 and 1 if initial is 2

BTW, this should not take more than a minute :wink:

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16232 reply_id: 60592[/import]

thank you for answers) its not exactly what i needed, but i figured it myself in the end [import]uid: 16142 topic_id: 16232 reply_id: 61068[/import]