2

Hi guys I'm new to lua and I'm having troubles of calculating every element of a tables but it only give me the last resulting calculation

open = {1,2,3,4,5}
close = {6,1,3,1,10}

It only returns

1 5

where it should be a table of each elements subtracted elements

here's my code I really need your help

o = {1,2,3,4,5}
c = {6,1,3,1,10}
for i = 1, #o do
   if c[i] >= o[i] then
      b = c[i] - o[i]
   else
      b = o[i] - c[i]
   end
   body ={}
        table.insert(body,1,b)
end
for key, value in ipairs(body) do print(key, value) end
Morbius
  • 29
  • 1
  • 4
    Move this line `body ={}` to the beginning of the code. – shingo Oct 14 '22 at 05:40
  • And avoid adding to the first index of an table to avoid shifting all entries every time. Unless you want to invert the result, you don't want this anyways. – Luke100000 Oct 14 '22 at 06:20

1 Answers1

0

Like @Piglet in the Comment wrote: 'you need to be very analytic about your work if you want to become a programmer.'

So for that analytic stuff you can use Lua standalone.
Give out what is relevant with print()...

> function walkover(open, close)                                                                                                                                                              
>> local open, close, body = open or {1, 2, 3, 4, 5}, close or {6, 1, 3, 1, 10}, setmetatable({}, {__index = table})                                                                          
>> print(#open .. ' Values in table open: ', body.concat(open, ', '))                                                                                                                         
>> print(#close .. ' Values in table close: ', body.concat(close, ', '))                                                                                                                      
>> print(#body .. ' Values in table body: ', body:concat(', '))                                                                                                                               
>> if #open == #close then for i=1, #open do if close[i] >= open[i] then body:insert(close[i] - open[i]) else body:insert(open[i] - close[i]) end end else error('Tables do not have same leng
th') end                                                                                                                                                                                      
>> print(#body .. ' Values in table body: ', body:concat(', '))                                                                                                                               
>> return body                                                                                                                                                                                
>> end                                                                                                                                                                                        
> walkover()                                                                                                                                                                                  
5 Values in table open:         1, 2, 3, 4, 5                                                                                                                                                 
5 Values in table close:        6, 1, 3, 1, 10                                                                                                                                                
0 Values in table body:                                                                                                                                                                       
5 Values in table body:         5, 1, 0, 3, 5                                                                                                                                                 
table: 0x5661e300                                                                                                                                                                             
> walkover({9,8,7,6,5},{4,3,2,1,0})                                                                                                                                                           
5 Values in table open:         9, 8, 7, 6, 5                                                                                                                                                 
5 Values in table close:        4, 3, 2, 1, 0                                                                                                                                                 
0 Values in table body:                                                                                                                                                                       
5 Values in table body:         5, 5, 5, 5, 5                                                                                                                                                 
table: 0x5661e7f0                                                                                                                                                                             
> walkover({9,8,7,6,5},{4,3,2,1,0,-1})                                                                                                                                                        
5 Values in table open:         9, 8, 7, 6, 5                                                                                                                                                 
6 Values in table close:        4, 3, 2, 1, 0, -1                                                                                                                                             
0 Values in table body:                                                                                                                                                                       
stdin:6: Tables do not have same length                                                                                                                                                       
stack traceback:                                                                                                                                                                              
        [C]: in function 'error'                                                                                                                                                              
        stdin:6: in function 'walkover'                                                                                                                                                       
        (...tail calls...)                                                                                                                                                                    
        [C]: in ?

This Example using the minimal error handling that checks the length of open against close and if equal do the math.

PS: If you dont understand the body constructor: setmetatable({}, {__index = table})
This Constructor adds all table Library function as methods.
This body will also returned with its metamethods.
So this is possible too with walkover()' return value...

print(walkover():concat(', '))
koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15