ascensio
|
  |
| Joined: 03 Aug 2013 |
| Total Posts: 498 |
|
|
| 15 Aug 2015 10:53 PM |
| I'm glad the enticing title worked. How do I apply (the same) arithmetic to every value in a table? For example, add one to each value. |
|
|
| Report Abuse |
|
|
hetebro
|
  |
| Joined: 29 Apr 2012 |
| Total Posts: 3708 |
|
|
| 15 Aug 2015 10:54 PM |
| There might be an easier way, but I'd loop through the table and just edit each value through the loop. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 15 Aug 2015 10:56 PM |
But you can do it "cleanly" with metatables.
local numArray = {0, 5, 10, 15}; setmetatable(numArray, { __add = function(self, value) local new = {}; for key = 1, #self do new[key] = self[key] + value; end return new; end; });
print(table.concat(numArray + 5, ", ")); |
|
|
| Report Abuse |
|
|
lupine
|
  |
| Joined: 24 Jun 2008 |
| Total Posts: 3561 |
|
|
| 15 Aug 2015 11:01 PM |
Pff, "cleanly". We're savages here, do this instead
local nums = {0, 2, 4, 8, 16, 32} for i,v in pairs(nums) do v = v * 2 end
|
|
|
| Report Abuse |
|
|
ascensio
|
  |
| Joined: 03 Aug 2013 |
| Total Posts: 498 |
|
|
| 15 Aug 2015 11:09 PM |
them's arrays but thank you |
|
|
| Report Abuse |
|
|