First, Lua and Wow's version of Lua no longer have table.getn, use #table instead. And while technically the same thing, an array in Lua is called a table, hence table.*.
local myTable = {1, 3, 5, 7}
for i = 1, #myTable do
if i + 1 > #myTable then
return -- exit out
end
local test_var = myTable[i] * myTable[i + 1]
print("Test var: %d", test_var)
end
You could get fancier with metatables, which won't inherently crash out when no value exists, and metatables do have __multiply components, among other basic math functions.
I am trying to multiply each element of an array list by its neighbor. I've tried to use a while loop to solve that issue.
As an example:
array_list = {1,3,5,7}
The result should be:
1 * 3 = 3
3 * 5 = 15
5 * 7 = 35
Code:
array_list = {1,3,5,7}
while array_list[i] do
if i <= table.getn(array_list) and i<=i+1 then
test_var = array_list[i] * array_list[i+1]
--term = math.sqrt(test_var)
end
end
print("Test var:" , test_var)
Do you have any suggestion ?
Best,
ecki891
Great job :)
Best,
ecki891