[Resolved] new to Lua.. this simple code with arrays makes me crazy..please help

Hi,
i’m new in lua and Corona, usually coding in delphi or vb,
now i’m trying this simple structure that
must read form a multidimensional array
that rappresents numbers of a lottery: tutteleestrazioni( draws , rows, numbers),
i want to scan entire array, then i increment all 90 numbers, rita[1…90] when draw changes
and i set rita[numeroinesame]=0 for all numbers i find when scanning the array of draws.
I want to calculate delay of numbers so at the end of this code… out of the scanning
cycles i expect to fine delay of numbers in 90 elements of rita[1…90] … but…
it happens that all elements are set at maximum number possibile…like if
all 90 elements have have been only incremented and never set to zero…
Thing that make me crazy is that inside the ‘for – next’ cycle the printing of
rita[] that is there for debugging is correct like this:
2000 8 86 0
2000 8 10 0 and so on… rita[86] and rita[10] are set to 0 as i expect
but out of the loop all elements of rita[] are … 4681 … the numbers of loops
and so all the increments done… never set to zero ?!
Please i know i’m doing somthing wrong with lua… but can’t understand…
can someone help me ?

Here is the code

function puliscearrayrita() --calling this create rita
rita = { }
local pos
for pos=1,90 do
rita[pos] = 0
end
end
function incrementaritordoditutti() – this function increments values for all elements in rita
for contatore=1, 90 do
rita[contatore]=rita[contatore]+1
end
end

– begin to scan array of three elements where i have loaded all draws
– external loop is now done with 'WHILE INDICE <= conteggiorighe (that is max dimension of indice just
–to try… it was the same doing another for…next

function calcolaritardi()
local j,z,contatore,numeroinesame
indice=1
while indice <= conteggiorighe do
incrementaritordoditutti() – call function that increment all elements in rita
for j=1,12 do
for z=1,5 do
numeroinesame = tutteleestrazioni[indice][j][z] – numeroinesame is the number found, catching all numbers

rita[numeroinesame] = 0 – i set ritato zero…here this works as i see printing
print(indice…" “…j…” “…numeroinesame…” "…rita[numeroinesame])

end

end

indice=indice+1
end – endwhile
end --endfunction
–HERE I CALL this functions written before
puliscearrayrita() – chiama pulisce il vettore che tiene i ritardi
calcolaritardi() – chiama la funzione che calcola i ritardi

–HERE I SCAN RITA… and i expect to see different values that are incremented delay of
–numbers… that’s because when found, each number has been set to zero and incrementing starts
–again, instead i see all elements of RITA incremented at maximum value of 4681…
– maybe a problem with scope but rita{} is global… so what ?

…Scanning rita[…90]
local i
for i = 1, 90 do
print (rita[i]) – rita elements are all 4681… what do i do wrong ?
end

Thank you… Roberto
[import]uid: 107268 topic_id: 28411 reply_id: 328411[/import]

Hey Roberto,

Just at a glance it appears you are saying;

while indice \<= conteggiorighe do

However I can’t see where conteggiorighe has been assigned any kind of value while indice = 1 - I would expect this to return a “attempt to compare number with nil” error. [import]uid: 52491 topic_id: 28411 reply_id: 114739[/import]

Hi Peach, i did not put all the lines,
anyway conteggiorighe’ is valid and valorized with maximum
value of INDICE then used in array tutteleestrazioni[indice][j][z]

so… everything works fine without error BUT… as i set
rita[numeroinesame] = 0 it’s valid and set to zero just there inside
the ‘for’ loop but is not so when the triple loop is end, so
if i check rita[anyindex] at the end i always have maximum number
of INDICE as it has been incremented for each cycle of it
ignoring the fact that some numbers have been set to zero
and incrementing did restart…

difficult to explain… maybe a mater with scope ?
do i do the right thing writing rita[myindex] = 0 without declaring
it again ? rita{ } is so declared only into the function that reset
all values for this table

Thank you. Roberto

[import]uid: 107268 topic_id: 28411 reply_id: 114762[/import]

I think that problem maybe
that
numeroinesame = tutteleestrazioni[indice][j][z]
is seen as string not number
then instruction rita[numeroinesame] = 0
does not set to zero the element numeroinesame of the array.

How can i force numeroinesame = tutteleestrazioni[indice][j][z] to be considered integer ?
…sorry if i write stupidity… but for me is new this language and sure i
move like elephant. Thank you. Roberto
[import]uid: 107268 topic_id: 28411 reply_id: 114822[/import]

SOLVED

doing like this

numeroinesame = tutteleestrazioni[indice][j][z]

numValue = tonumber(numeroinesame)

rita[numValue] = 0

so numeroinesame was treated like a string also if it’s always a number
now it works

Thank you. Roberto [import]uid: 107268 topic_id: 28411 reply_id: 114845[/import]

Hey Roberto - sorry, we’ve got a bit of a time difference.

Very pleased to hear that you got this solved - thank you for sharing your solution, I’m sure it will help others in the future :slight_smile: [import]uid: 52491 topic_id: 28411 reply_id: 114908[/import]