This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

For ipairs for table differences
#1
Hi ,

I have 3 tables like. Those are the Group names.

temp = { 'Temp 1', 'Temp 2', 'Temp 3' }
diff = { 'Diff 1', 'Diff 2', 'Diff 3' }
result = { 'Result 1', 'Result 2', 'Result 3' }


I need to calculate
Temp 1 - Diff 1 and write to Result 1

How can i do via for ipairs loop 

Regards,
Reply
#2
Hi Try like this 

Code:
temp = { '20', '22', '5' }
diff = { '17', '18', '19' }

result ={}

for i = 1, #temp do
 
  result[i] =  temp[i] - diff[i]
 
end

log(result)
Reply
#3
Use this code if each table member is a group address or an object name. Make sure that each table has the same number of elements.
Code:
temp = { 'Temp 1', 'Temp 2', 'Temp 3' }
diff = { 'Diff 1', 'Diff 2', 'Diff 3' }
result = { 'Result 1', 'Result 2', 'Result 3' }

for i, res in ipairs(result) do
  val = grp.getvalue(temp[ i ]) - grp.getvalue(diff[ i ])
  grp.write(res, val)
end
Reply


Forum Jump: