| View previous topic :: View next topic |
| Author |
Message |
icsmoke+ Cheater
Reputation: 0
Joined: 31 Aug 2020 Posts: 29
|
Posted: Wed Mar 02, 2022 2:58 am Post subject: [ask] help to understanding iterator |
|
|
hello there master, please help me to understanding about iterator, i have code below that makes me confused. here's the code
| Code: |
number = 50
a1 = 1
a2 = 50
a3 = 2
a4 = 50
number1 = 100
aa1 = 1
aa2 = 100
aa3 = 300
aa4 = 100
function k1() a = a1 + aa1 end
function k2() a = a2 + aa2 end
function k3() a = a3 + aa4 end
function k4() a = a4 + aa4 end
donestate = false
function z()
for i = 1, 4 do
if donestate == false then
if 'a'..i == number and -- check if the value is true or false from a1 - a4
'aa'..i == number1 then -- check if the value is true or false from aa1 - aa4
print('right')
print('a'..i)
print('aa'..i)
('k'..i)() -- doing something when check is true from function k1 - k4()
donestate = true
else
print('wrong')
donestate = false
end
end
end
end
|
thanks in advance
|
|
| Back to top |
|
 |
LeFiXER Grandmaster Cheater Supreme
Reputation: 20
Joined: 02 Sep 2011 Posts: 1069 Location: 0x90
|
Posted: Wed Mar 02, 2022 4:58 am Post subject: |
|
|
An iterator is used in loops for example:
| Code: |
for i = 0, 10 do
print('The loop is currently at ' .. tostring(i))
end
|
In this code, i would be the iterator.
As for your code, the condition will never succeed because 'a' is treated as a string, it will never be a number. What is it you're trying to achieve?
|
|
| Back to top |
|
 |
AylinCE Grandmaster Cheater Supreme
Reputation: 37
Joined: 16 Feb 2017 Posts: 1531
|
Posted: Wed Mar 02, 2022 5:45 am Post subject: |
|
|
Interpret variables in a global table.
In this way, it is possible to reach them from a wider angle.
| Code: | local scope={}
number = 50
scope.a1 = 1
scope.a2 = 50
scope.a3 = 2
scope.a4 = 50
number1 = 100
scope.aa1 = 1
scope.aa2 = 100
scope.aa3 = 300
scope.aa4 = 100
function scope.k1() a = scope.a1 + scope.aa1 return tonumber(a) end
function scope.k2() a = scope.a2 + scope.aa2 return tonumber(a) end
function scope.k3() a = scope.a3 + scope.aa4 return tonumber(a) end
function scope.k4() a = scope.a4 + scope.aa4 return tonumber(a) end
donestate = false
function z()
for i = 1, 4 do
if donestate == false then
if scope['a'..i] == number and -- check if the value is true or false from a1 - a4
scope['aa'..i] == number1 then -- check if the value is true or false from aa1 - aa4
print('right: ' .. 'a' .. i .. ' - aa' .. i)
aFind1=tonumber(scope['a'..i])
aFind2=tonumber(scope['aa'..i])
print(aFind1)
print(aFind2)
aFind3=(scope['k'..i])() -- doing something when check is true from function k1 - k4()
print("Find: " .. aFind3)
donestate = true
else
print('wrong: ' .. 'a' .. i .. ' - aa' .. i)
donestate = false
end
end
end
end
z() |
_________________
|
|
| Back to top |
|
 |
|