array length

local arr1 = {0,0,0,0,0} local i = 1 print(#arr..i)

How to get the length of arr1 ? 

In the snippet you posted, if you change print(#arr…i) to print(#arr1), it will print the length of arr1 (5)

 local level1 = {0,0,0,0,0} local level2 = {0,0,0} local function onTouch(e) local function scaleUp() transition.to(e.target, {time = 100, xScale = 1, yScale = 1}) end if e.phase == "began" then transition.to(e.target, {time = 100, xScale = .8, yScale = .8, onComplete=scaleUp}) id = e.target.id print(#level..id) elseif e.phase == "ended" then end end for i = 1 ,2 do arr ={} arr[i] = display.newRect(0,0,80,80) arr[i].x = i\*120 arr[i].y = 200 arr[i].id = i arr[i]:addEventListener("touch",onTouch) end

I want to print the length of  level1 when i touch the first block and print the length of level2 when i touch the second block . How can I do that ?

As memo stated above its literally just print(#level1), just call it in your touch listener for the first block and do the same for level 2/block 2

i think what you’re asking is:  how to *dynamically* build (and access) the variable names “layer1”,“layer2”, right?

first option, just forget about it, and use conditional logic instead, fe:

if (id==1) then   print(#layer1) elseif (id==2) then   print(#layer2) end

second option, you’ll need a “wrapper” for your layers, then you can access the wrapper by dynamic name, fe:

local layer1 = {1,1,1,1,1} local layer2 = {2,2,2} local layers  = { layer1=layer1, layer2=layer2 } print(#layers["layer"..1]) print(#layers["layer"..2])

hth

In the snippet you posted, if you change print(#arr…i) to print(#arr1), it will print the length of arr1 (5)

 local level1 = {0,0,0,0,0} local level2 = {0,0,0} local function onTouch(e) local function scaleUp() transition.to(e.target, {time = 100, xScale = 1, yScale = 1}) end if e.phase == "began" then transition.to(e.target, {time = 100, xScale = .8, yScale = .8, onComplete=scaleUp}) id = e.target.id print(#level..id) elseif e.phase == "ended" then end end for i = 1 ,2 do arr ={} arr[i] = display.newRect(0,0,80,80) arr[i].x = i\*120 arr[i].y = 200 arr[i].id = i arr[i]:addEventListener("touch",onTouch) end

I want to print the length of  level1 when i touch the first block and print the length of level2 when i touch the second block . How can I do that ?

As memo stated above its literally just print(#level1), just call it in your touch listener for the first block and do the same for level 2/block 2

i think what you’re asking is:  how to *dynamically* build (and access) the variable names “layer1”,“layer2”, right?

first option, just forget about it, and use conditional logic instead, fe:

if (id==1) then   print(#layer1) elseif (id==2) then   print(#layer2) end

second option, you’ll need a “wrapper” for your layers, then you can access the wrapper by dynamic name, fe:

local layer1 = {1,1,1,1,1} local layer2 = {2,2,2} local layers  = { layer1=layer1, layer2=layer2 } print(#layers["layer"..1]) print(#layers["layer"..2])

hth