lostend
|
  |
| Joined: 21 Aug 2011 |
| Total Posts: 8265 |
|
|
| 22 Aug 2015 01:38 PM |
and i figured out metamethods have a double underscore. >.< so ive been doing this: _add, but it is actually this: __add
well anyways, this is what I made. pretty simple, metatables are a LOT easier than most would think. this allows you to perform arithmetic on tables
local tab1={5,7,'Hello','MOM OLLEH'} local tab2={1,2,8,'SELBAT','NAMTAB'}
function getTotals(a,b) local totals={0,0} for i=1,#(#a>=#b and a or #b>#a and b) do totals[1]=totals[1]+(tonumber(a[i]) or 0) totals[2]=totals[2]+(tonumber(b[i]) or 0) end return totals end
local arithmetic={ __add=function(a,b) local totals=getTotals(a,b) return totals[1]+totals[2] end, __mul=function(a,b) local totals=getTotals(a,b) return totals[1]*totals[2] end, __div=function(a,b) local totals=getTotals(a,b) return totals[1]/totals[2] end, __sub=function(a,b) local totals=getTotals(a,b) return totals[1]-totals[2] end, __mod=function(a,b) local totals=getTotals(a,b) return totals[1]%totals[2] end, __pow=function(a,b) local totals=getTotals(a,b) return totals[1]^totals[2] end, __unm=function(a) local new_Table={} for i=1,#a do if type(a[i])=='number' then new_Table[#new_Table+1]=-(a[i]) elseif type(a[i])=='string' then new_Table[#new_Table+1]=(a[i]:reverse()) end end return new_Table end }
setmetatable(tab1,arithmetic) setmetatable(tab2,arithmetic)
print(tab1+tab2) print(tab1*tab2) print(tab1/tab2) print(tab1-tab2) print(tab1%tab2) print(tab1^tab2) print(table.concat(-tab1,' ')) print(table.concat(-tab2,' '))
|
|
|
| Report Abuse |
|
lostend
|
  |
| Joined: 21 Aug 2011 |
| Total Posts: 8265 |
|
| |
lokkut
|
  |
| Joined: 18 Mar 2009 |
| Total Posts: 1794 |
|
|
| 22 Aug 2015 07:31 PM |
| what's the point in this thread? |
|
|
| Report Abuse |
|
ShungTzu
|
  |
| Joined: 14 Jun 2014 |
| Total Posts: 959 |
|
|
| 22 Aug 2015 07:43 PM |
| Holly mother of tables, Batman! |
|
|
| Report Abuse |
|