Logic Machine Forum
For ipairs for table differences - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: For ipairs for table differences (/showthread.php?tid=3044)



For ipairs for table differences - savaskorkmaz - 03.12.2020

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,


RE: For ipairs for table differences - benanderson_475 - 04.12.2020

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)



RE: For ipairs for table differences - admin - 04.12.2020

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