i have problem with Lua goto. An error occurs after running the script : undefined label 'back'
Code:
function determinant()
det = 1
for k = n, 2, -1 do
if a[k][k] == 0 then
goto skok_jedna
end
::back::
for l = 1, k-1 do
for m = 1, k-1 do
a[l][m] = a[l][m] - a[l][k] * a[k][m] / a[k][k]
end
end
det = det * a[k][k]
end
det = det * a[1][1]
goto konec_det
::skok_jedna::
l = k
::skok_dva::
l = l - 1
if l == 0 then
det = 0
return
end
if a[l][k] == 0 then
goto skok_dva
end
for m = 1, k do
c[n] = a[l][m]
a[l][m] = a[k][m]
a[k][m] = c[m]
end
det = det * -1
goto back
::konec_det::
end
The script is part of a program to calculate a system of linear equations of n-variables using Cramer's rule.
Where is problem ? Thx all for reply
CodePudding user response:
The Lua manual says:
- A label is visible in the entire block where it is defined, except inside nested functions.
- A goto may jump to any visible label as long as it does not enter into the scope of a local variable.
What's going on in your code:
- The label
back
is invisible from the linegoto back
- You're jumping inside the scope of loop variable
k
CodePudding user response:
Thx for reply. I solve this problem with second function (remove goto from for-loop and goto back to for-loop):
function determinant()
det = 1
for k = n, 2, -1 do
if a[k][k] == 0 then
det2 {k}
if det == 0 then
return
end
end
for l = 1, k-1 do
for m = 1, k-1 do
a[l][m] = a[l][m] - a[l][k] * a[k][m] / a[k][k]
end
end
det = det * a[k][k]
end
det = det * a[1][1]
end
function det2(k)
l = k
::skok_dva::
l = l - 1
if l == 0 then
det = 0
return
end
if a[l][k] == 0 then
goto skok_dva
end
for m = 1, k do
c[n] = a[l][m]
a[l][m] = a[k][m]
a[k][m] = c[m]
end
det = det * -1
end