problem with table ( please help me )

Hi eveyone,

i make a small application with using table object , but unfortanotly it gave wrong .

this is the code :

local widget = require( "widget" ) local json = require("json") local group = display.newGroup() local btnstable = {} local page = 1 local token = "Z7Tj-iJ6EtNSd2WDnQiqTAXgKQAIVXf9hgEH1NnBv\_4GWzUFW7MloCYrZ3\_Y6N84pmPztfcTrZkKu0JHm-HcIBlSABi54HZw4-2E73xgJwMkUtuO34h8fkGI7A1ImqGbtEJT2UTdtmDT1tOpxBpc9eDzzqCRRq1TZR6rH2uMPc97AyqWQ3s\_mHiNLmhZ705U4Cr\_iaZRKgtwDKugTcvM5eokrXalr36aBEYNGd43CgWgSj1rnWXRr9G5Hxn3Tudg9X0IyCVgm6jYa8GiquKRwgf\_X7octzaj8Ho4jHi\_IlqSvnAHIWxL4oy0cfaLtSC\_jmJILPF5c541CDEZJIZUaw8pofjpUDScDzxzHeVw45HT-JLC7ASAsBfNbB2DYAUlpGNKHb44KCFRd6gYZoohX\_CrySQHX6onY7nIXD9hI6aRWeLuF3rWb01HdCGdfJkauqImbC-k-VgjdtQIl619KGYSOnpAu5WtPovGmsX3XBySE5ny0HhD-lXFYfSbimGc" gid = 1 json1= [[{"stlst":[{"sid":6,"name":"a","lvl":2,"dcount":0},{"sid":7,"name":"b","lvl":2,"dcount":0},{"sid":8,"name":"c","lvl":1,"dcount":0},{"sid":9,"name":"d","lvl":1,"dcount":0},{"sid":10,"name":"e","lvl":1,"dcount":0},{"sid":11,"name":"f","lvl":1,"dcount":0},{"sid":12,"name":"g","lvl":1,"dcount":0},{"sid":13,"name":"h","lvl":3,"dcount":0},{"sid":14,"name":"j","lvl":1,"dcount":0},{"sid":17,"name":"k","lvl":1,"dcount":0},{"sid":18,"name":"l","lvl":1,"dcount":0},{"sid":19,"name":"m","lvl":1,"dcount":0},{"sid":20,"name":"n","lvl":1,"dcount":0},{"sid":21,"name":"o","lvl":1,"dcount":0},{"sid":22,"name":"p","lvl":2,"dcount":0}]}]] json2= [[{"stlst":[{"sid":23,"name":"q","lvl":3,"dcount":0},{"sid":24,"name":"r","lvl":1,"dcount":0},{"sid":2,"name":"s","lvl":1,"dcount":1},{"sid":4,"name":"t","lvl":1,"dcount":1},{"sid":5,"name":"u","lvl":1,"dcount":1},{"sid":1,"name":"v","lvl":1,"dcount":1}]} ]] display.setDefault( "background", 1, 1, 1 ) local function rbtnEvent( event ) if ( "ended" == event.phase ) then local data if ( event.target.id == "btn1" ) then data = json.decode(json1) elseif ( event.target.id == "btn2" ) then data = json.decode(json2) end for key,value in pairs(btnstable) do print("before " ..key) end for v in pairs(data["stlst"]) do if(btnstable[data["stlst"][v]["sid"]] == null) then print("add x "..data["stlst"][v]["sid"]) table.insert( btnstable, data["stlst"][v]["sid"],{ isdbefore = 0 , stat = false } ) end if(btnstable[data["stlst"][v]["sid"]] == null) then print("add z "..data["stlst"][v]["sid"]) table.insert( btnstable, data["stlst"][v]["sid"],{ isdbefore = 1 , stat = false } ) end end for key,value in pairs(btnstable) do print("after " ..key) end end end local btn1 = widget.newButton { id = "btn1", width = 35, height = 35, label = "btn1", onEvent = rbtnEvent } btn1.x = display.contentCenterX + 90 btn1.y = display.contentCenterY +140 group:insert( btn1 ) local btn2 = widget.newButton { id = "btn2", width = 35, height = 35, label = "btn2", onEvent = rbtnEvent } btn2.x = display.contentCenterX - 90 btn2.y = display.contentCenterY +140 group:insert( btn2 ) 

when i press the btn1

it shows

thats good.

but when i press btn2

is shows

as u can see it should add the keys ( 23 , 24 ,2,4,5,1) to the prevous records

but it gave me additional records for example keys ( 25 ,26,3,15,16) as it shown before

please help me becuse its serious issue

many thanks

Why are you hand encoding tables as json?  That is just asking for trouble.

If you have just a few values, make the tables manually:

local huh = {} huh.stlst = {} huh.stlst.sid = 6 huh.stlst.name = "a" huh.stlst.lvl = 2 hug.dcount = 0

If you have a lot of values, be smart and do it programmatically:

local function addRecord( parent, recordName, fields ) local tmp = {} parent[recordName] = tmp for k, v in pairs( fields ) tmp[k] = v end end -- Now create blank table -- local huh = {} -- Now fill it -- addRecord( huh, "stlist1", { sid = 6, name = "a", lvl = 2, dcount = 0, friend = "d" } ) addRecord( huh, "stlist2", { sid = 7, name = "b", lvl = 2, dcount = 2 } ) addRecord( huh, "stlist3", { sid = 100, name = "c", lvl = 2, dcount = 10 } ) addRecord( huh, "stlist4", { sid = 2, name = "d", lvl = 2, dcount = 0, friend = "a" } )

Oh, and reformat your code before pasting to a code block.

  1. Paste it to notepad (or the equivalent on your OS of choice).

  2. Globally replace the tabs with 2 or 3 spaces (to save width).

  3. Grab that code and paste it as  code block here.

While I very much appreciate that you tried to post that code as a code block… its totally illegible with those tabs.  No big deal, live and learn.  I forget about tabs sometimes too.

Cheers,

Ed

Just noticed this in your code too:

for v in pairs(data["stlst"]) do

That is not the right construct.  This is:

for k,v in pairs(data["stlst"]) do

Also, its hard to read, but it looks like you’re doing this:

for key,value in pairs( someTable ) do for key,value in pairs( value ) do end end

While you may get away with same named indexes in some rare cases,  that is wrong too.  Don’t embed iterators with the same names as their parents iterators…

Do this instead:

for key1,value1 in pairs( someTable ) do for key2,value2 in pairs( value1 ) do end end

Hi , excuse me i think u did not see my code correctly. in my code each for statment is seprate .

and as i told there is a bug in table librery.

u can check this code and u will see if it as i say or not :

local function btnEvent( event ) if ( "ended" == event.phase ) then if ( event.target.id == "btn1" ) then table.insert( btnstable, 6," " ) table.insert( btnstable, 7, " " ) table.insert( btnstable, 8, " " ) table.insert( btnstable, 9," " ) table.insert( btnstable, 5," " ) elseif ( event.target.id == "btn2" ) then table.insert( btnstable, 23," " ) table.insert( btnstable, 24," " ) table.insert( btnstable, 2," " ) table.insert( btnstable, 4," " ) table.insert( btnstable, 1," " ) end for key,value in pairs(btnstable) do print(key) end end end local btn = widget.newButton { id = "btn", width = 35, height = 35, label = "btn", onEvent = btnEvent } btn.x = display.contentCenterX btn.y = display.contentCenterY +180 group:insert( btn )

        

looking for answer .

sorry i mean this code :

local function btnEvent( event ) if ( "ended" == event.phase ) then if ( event.target.id == "btn1" ) then table.insert( btnstable, 6," " ) table.insert( btnstable, 7, " " ) table.insert( btnstable, 8, " " ) table.insert( btnstable, 9," " ) table.insert( btnstable, 5," " ) elseif ( event.target.id == "btn2" ) then table.insert( btnstable, 23," " ) table.insert( btnstable, 24," " ) table.insert( btnstable, 2," " ) table.insert( btnstable, 4," " ) table.insert( btnstable, 1," " ) end for key,value in pairs(btnstable) do print(key) end end end local btn1 = widget.newButton { id = "btn1", width = 35, height = 35, label = "btn1", onEvent = btnEvent } btn1.x = display.contentCenterX btn1.y = display.contentCenterY +100 group:insert( btn1 ) local btn2 = widget.newButton { id = "btn2", width = 35, height = 35, label = "btn2", onEvent = btnEvent } btn2.x = display.contentCenterX btn2.y = display.contentCenterY +150 group:insert( btn2 )

when btn1 pressed it shows :

Nov 15 02:15:26.524: 6
Nov 15 02:15:26.524: 7
Nov 15 02:15:26.524: 8
Nov 15 02:15:26.525: 9
Nov 15 02:15:26.525: 5

when btn2 pressed it shows :

Nov 15 02:15:53.671: 1
Nov 15 02:15:53.671: 3
Nov 15 02:15:53.671: 5
Nov 15 02:15:53.671: 6
Nov 15 02:15:53.671: 7
Nov 15 02:15:53.672: 8
Nov 15 02:15:53.672: 24
Nov 15 02:15:53.672: 10
Nov 15 02:15:53.672: 9
Nov 15 02:15:53.672: 23

so as u see there is 3,10 !

looking for answer

many thanks

There is no bug.   pairs() iterates results it any order it wants, not necessarily indexed order.

If you want indexed order do this:

for i = 1, #btnstable do print(i, btnstable[i]) end

i am sorry for being late.

it couldn’t be solved.

when i trying this code :

print(btnstable[3]) print(btnstable[10])

it gave me there is data !

but as u see in previous code i did not add data with keys (3,10).

Can someone else answer this.  I don’t use the insert function much or make sparse indexed tables.

However I would do this:

btnstable[6] = " " btnstable[7] = " " btnstable[8] = " " btnstable[9] = " " btnstable[5] = " "

Not this:

table.insert( btnstable, 6," " ) table.insert( btnstable, 7, " " ) table.insert( btnstable, 8, " " ) table.insert( btnstable, 9," " ) table.insert( btnstable, 5," " )

Oh, and you can’t iterate over a sparse table.

i.e. This won’t work as you might think:

btnstable[1] = " " btnstable[2] = " " btnstable[4] = " " for i = 1, #btnstable do print( btnstable[i] ) end

Do this instead:

for i = 1, 4 do print( btnstable[i] ) end

if you REALLY want a sparse table, then instead of insert(), do this:

t[3] = “whatever”

t[23] = “whatever”

t[93] = “whatever”

for k,v in pairs(t) do print(k,v) end

conversely, if you DON’T want a sparse table, then index it sequentially and iterate with for i=1,#t or ipairs()

right now you have half of one and half of the other, and that won’t work the way you seem to expect.  it’s not a bug in tables, it’s documented behavior - you just need to figure out if you want to treat your table as an “array” (with sequential numeric keys) or as a “hash” (with arbitrary keys), then populate and iterate appropriately.  if you mix the two, then the #count of the table becomes nonsense, so things like insert() won’t behave as you seem to expect.

ok thats work good.

many thanks :slight_smile: :slight_smile: :slight_smile:

best regards.

Why are you hand encoding tables as json?  That is just asking for trouble.

If you have just a few values, make the tables manually:

local huh = {} huh.stlst = {} huh.stlst.sid = 6 huh.stlst.name = "a" huh.stlst.lvl = 2 hug.dcount = 0

If you have a lot of values, be smart and do it programmatically:

local function addRecord( parent, recordName, fields ) local tmp = {} parent[recordName] = tmp for k, v in pairs( fields ) tmp[k] = v end end -- Now create blank table -- local huh = {} -- Now fill it -- addRecord( huh, "stlist1", { sid = 6, name = "a", lvl = 2, dcount = 0, friend = "d" } ) addRecord( huh, "stlist2", { sid = 7, name = "b", lvl = 2, dcount = 2 } ) addRecord( huh, "stlist3", { sid = 100, name = "c", lvl = 2, dcount = 10 } ) addRecord( huh, "stlist4", { sid = 2, name = "d", lvl = 2, dcount = 0, friend = "a" } )

Oh, and reformat your code before pasting to a code block.

  1. Paste it to notepad (or the equivalent on your OS of choice).

  2. Globally replace the tabs with 2 or 3 spaces (to save width).

  3. Grab that code and paste it as  code block here.

While I very much appreciate that you tried to post that code as a code block… its totally illegible with those tabs.  No big deal, live and learn.  I forget about tabs sometimes too.

Cheers,

Ed

Just noticed this in your code too:

for v in pairs(data["stlst"]) do

That is not the right construct.  This is:

for k,v in pairs(data["stlst"]) do

Also, its hard to read, but it looks like you’re doing this:

for key,value in pairs( someTable ) do for key,value in pairs( value ) do end end

While you may get away with same named indexes in some rare cases,  that is wrong too.  Don’t embed iterators with the same names as their parents iterators…

Do this instead:

for key1,value1 in pairs( someTable ) do for key2,value2 in pairs( value1 ) do end end

Hi , excuse me i think u did not see my code correctly. in my code each for statment is seprate .

and as i told there is a bug in table librery.

u can check this code and u will see if it as i say or not :

local function btnEvent( event ) if ( "ended" == event.phase ) then if ( event.target.id == "btn1" ) then table.insert( btnstable, 6," " ) table.insert( btnstable, 7, " " ) table.insert( btnstable, 8, " " ) table.insert( btnstable, 9," " ) table.insert( btnstable, 5," " ) elseif ( event.target.id == "btn2" ) then table.insert( btnstable, 23," " ) table.insert( btnstable, 24," " ) table.insert( btnstable, 2," " ) table.insert( btnstable, 4," " ) table.insert( btnstable, 1," " ) end for key,value in pairs(btnstable) do print(key) end end end local btn = widget.newButton { id = "btn", width = 35, height = 35, label = "btn", onEvent = btnEvent } btn.x = display.contentCenterX btn.y = display.contentCenterY +180 group:insert( btn )

        

looking for answer .

sorry i mean this code :

local function btnEvent( event ) if ( "ended" == event.phase ) then if ( event.target.id == "btn1" ) then table.insert( btnstable, 6," " ) table.insert( btnstable, 7, " " ) table.insert( btnstable, 8, " " ) table.insert( btnstable, 9," " ) table.insert( btnstable, 5," " ) elseif ( event.target.id == "btn2" ) then table.insert( btnstable, 23," " ) table.insert( btnstable, 24," " ) table.insert( btnstable, 2," " ) table.insert( btnstable, 4," " ) table.insert( btnstable, 1," " ) end for key,value in pairs(btnstable) do print(key) end end end local btn1 = widget.newButton { id = "btn1", width = 35, height = 35, label = "btn1", onEvent = btnEvent } btn1.x = display.contentCenterX btn1.y = display.contentCenterY +100 group:insert( btn1 ) local btn2 = widget.newButton { id = "btn2", width = 35, height = 35, label = "btn2", onEvent = btnEvent } btn2.x = display.contentCenterX btn2.y = display.contentCenterY +150 group:insert( btn2 )

when btn1 pressed it shows :

Nov 15 02:15:26.524: 6
Nov 15 02:15:26.524: 7
Nov 15 02:15:26.524: 8
Nov 15 02:15:26.525: 9
Nov 15 02:15:26.525: 5

when btn2 pressed it shows :

Nov 15 02:15:53.671: 1
Nov 15 02:15:53.671: 3
Nov 15 02:15:53.671: 5
Nov 15 02:15:53.671: 6
Nov 15 02:15:53.671: 7
Nov 15 02:15:53.672: 8
Nov 15 02:15:53.672: 24
Nov 15 02:15:53.672: 10
Nov 15 02:15:53.672: 9
Nov 15 02:15:53.672: 23

so as u see there is 3,10 !

looking for answer

many thanks

There is no bug.   pairs() iterates results it any order it wants, not necessarily indexed order.

If you want indexed order do this:

for i = 1, #btnstable do print(i, btnstable[i]) end

i am sorry for being late.

it couldn’t be solved.

when i trying this code :

print(btnstable[3]) print(btnstable[10])

it gave me there is data !

but as u see in previous code i did not add data with keys (3,10).

Can someone else answer this.  I don’t use the insert function much or make sparse indexed tables.

However I would do this:

btnstable[6] = " " btnstable[7] = " " btnstable[8] = " " btnstable[9] = " " btnstable[5] = " "

Not this:

table.insert( btnstable, 6," " ) table.insert( btnstable, 7, " " ) table.insert( btnstable, 8, " " ) table.insert( btnstable, 9," " ) table.insert( btnstable, 5," " )