[Resolved] length of a array??

hello…

how can i get the length of an array??

this is my code…

local function loginCallback(event)
if ( event.isError ) then
print( “Network error!”)
else
print ( "RESPONSE: " … event.response )
local data={};
data = json.decode(event.response)
– do with data what you want…
if (data.result == 200) then
– player logged in okay
print(“Success”)
print(#data.rows)
y=70;
for i = 1, #data.rows do – this return 0, HERE IS MY PROBLEM
print(data.rows[tostring(i)].id)
print(data.rows[tostring(i)].name)
print(data.rows[tostring(i)].surname)
print(data.rows[tostring(i)].image_url)
y=y+30;
local text2 = display.newText( data.rows[tostring(i)].id, 0, 0, native.systemFontBold, 18)
text2:setTextColor( 0 )
text2:setReferencePoint( display.CenterReferencePoint )
text2.x, text2.y = display.contentWidth * 0.5, y
group:insert( text2 )

y=y+30;
local text3 = display.newText( data.rows[tostring(i)].name, 0, 0, native.systemFontBold, 18)
text3:setTextColor( 0 )
text3:setReferencePoint( display.CenterReferencePoint )
text3.x, text3.y = display.contentWidth * 0.5, y
group:insert( text3 )

y=y+30;
local text4 = display.newText( data.rows[tostring(i)].surname, 0, 0, native.systemFontBold, 18)
text4:setTextColor( 0 )
text4:setReferencePoint( display.CenterReferencePoint )
text4.x, text4.y = display.contentWidth * 0.5, y
group:insert( text4 )
local function networkListener( event )
if ( event.isError ) then
print ( “Network error - download failed” )
end
end
local img = display.loadRemoteImage(data.rows[tostring(i)].image_url, “GET”, networkListener, “localcopy.jpg”, system.TemporaryDirectory,_W/4,200)

end
else
– prompt them to login again
end

end
return true
end

local URL = “http://www.optisoft.gr/corona/index.php
network.request( URL, “GET”, loginCallback )
[import]uid: 185094 topic_id: 32109 reply_id: 332109[/import]

Lua tables have two “modes” for a lack of a better term. They can have numeric indexes:

t[1] = “Hello World”
t[2] = “How are you?”

or they can be more like a PHP Associative Array or Perl hash where you use key’s to store things (key-value pairs). There are also two ways of specifying the keys to make things even more fun.

t[“firstmsg”] = “Hello World”
t[“secondmsg”] = “How are you?”

or alternatively:

t.firstmsg = “Hello World”
t.secondmsg = “How are you?”

Those are in effect identical.

The # operator will not work with the key-value style of tables, it only works with numeric indexed arrays. However it also stops counting when it hits a nil value:

t[1] = “Hello World”
t[2] = “How are you?”
t[3] = nil
t[4] = “Top of the mornin’ to ya!”

#t is 2 not 4. If you use #tablename and the count is less than you expect, you likely have a nil in the table. The API function:

table.maxn(t)

will in this case produce a value of 4. Keep in mind this also only works on numerically indexed tables, not the key-value type. You would have to write your own count function using:

count = 0  
for k,v in pairs(t) do  
 count = count + 1  
end  

to get the count of tables that are not numerically indexed.
[import]uid: 19626 topic_id: 32109 reply_id: 127899[/import]

thanks again rob! [import]uid: 185094 topic_id: 32109 reply_id: 127904[/import]

Lua tables have two “modes” for a lack of a better term. They can have numeric indexes:

t[1] = “Hello World”
t[2] = “How are you?”

or they can be more like a PHP Associative Array or Perl hash where you use key’s to store things (key-value pairs). There are also two ways of specifying the keys to make things even more fun.

t[“firstmsg”] = “Hello World”
t[“secondmsg”] = “How are you?”

or alternatively:

t.firstmsg = “Hello World”
t.secondmsg = “How are you?”

Those are in effect identical.

The # operator will not work with the key-value style of tables, it only works with numeric indexed arrays. However it also stops counting when it hits a nil value:

t[1] = “Hello World”
t[2] = “How are you?”
t[3] = nil
t[4] = “Top of the mornin’ to ya!”

#t is 2 not 4. If you use #tablename and the count is less than you expect, you likely have a nil in the table. The API function:

table.maxn(t)

will in this case produce a value of 4. Keep in mind this also only works on numerically indexed tables, not the key-value type. You would have to write your own count function using:

count = 0  
for k,v in pairs(t) do  
 count = count + 1  
end  

to get the count of tables that are not numerically indexed.
[import]uid: 19626 topic_id: 32109 reply_id: 127899[/import]

thanks again rob! [import]uid: 185094 topic_id: 32109 reply_id: 127904[/import]