Table Insertion problem...

  I am storing names & scores for a math app using a table and then sorting the table based on score.

  The insertion works fine on the FIRST insertion then does nothing after the second game is over and

   the SECOND insertion is attempted.

   The original table is populated with 10 names and 10 scores:

ScoresTable = {

        { name=“Einstein”, score=999 },

        { name=“Fibonacci”, score=804 },

        { name=“Fermat”, score=596 },

        { name=“Laplace”, score=712},

        { name=“Newton”, score=3__99},

        { name=“Pythagoras”, score=153},

        { name=“Archimedes”, score=99},

        { name="—", score=0},

        { name="—", score=0},

        { name="—", score=0},

}

   After playing the first game, the student’s name score is inserted (as the 11th name & score) and table is sorted by score…

table.insert( ScoresTable, 11, { name=StudentName, score=StudentScore } )

local sort_func = function( a,b ) return a.score > b.score end

table.sort( ScoresTable, sort_func )

   So far, everything works great. I switch the 3rd & 4th scores to confirm a successful sort.

   I extract the top 10 names and scores and display them and get something like this…

Math Scores

Einstein          999 

Fibonacci       804 

Laplace          712   <<< sort is working

Fermat           596 

Newton          3__99

Pythagoras     153

Archimedes     99

Jeremy            39   <<< new student & score

      —                0

      —                0

   Notice Laplace is now above Fermat after sort.

   Then the student plays a second game and returns to the “scores page”.

   A second identical insert of Student’s name & score is attempted with no change in scores.

   What gives???

   I tried removing table and setting it to nil and then repopulate it… 

ScoresTable:removeSelf() 

ScoresTable = nil

   No change.

   I also tried inserting name & score as the 11th, then 12th, 13th…etc. after each game.

   No change.

   I hope someone has a clue as to why I can only insert once into my scores table.

   The ScoreTable is global established on storyboard 1.

   Student’s name is added on storyboard 2

   Game is played on storyboard 3.

   Scores are on storyboard 4.

   The scenes of each storyboard are purged on next storyboard.

   Thanks in advance for any help.

   Mark McCurry

Hi Mark,

First, FYI, ScoresTable:removeSelf() wouldn’t do anything.  The :removeSelf() API is for Corona display objects, not for plain old Lua tables.  ScoresTable=nil, of course, would remove the reference to the table.

In any case, the best way to debug something like this is with lots of print statements.  Right before you insert an element into the table, print out the data you’re inserting, to make sure it’s what you think it is.  Perhaps the code we’re you think you’re inserting into the table the second time isn’t even running, which would explain why the table isn’t changing.  A print statement would reveal that immediately, since the print wouldn’t show up in your console log.

  • Andrew

Andrew,

   Thanks for the information. It was very helpful.

   I was able to correct the multiple insert problem by

   making it a function and then calling the function…

Mark McCurry

SoftSeven

Hi Mark,

First, FYI, ScoresTable:removeSelf() wouldn’t do anything.  The :removeSelf() API is for Corona display objects, not for plain old Lua tables.  ScoresTable=nil, of course, would remove the reference to the table.

In any case, the best way to debug something like this is with lots of print statements.  Right before you insert an element into the table, print out the data you’re inserting, to make sure it’s what you think it is.  Perhaps the code we’re you think you’re inserting into the table the second time isn’t even running, which would explain why the table isn’t changing.  A print statement would reveal that immediately, since the print wouldn’t show up in your console log.

  • Andrew

Andrew,

   Thanks for the information. It was very helpful.

   I was able to correct the multiple insert problem by

   making it a function and then calling the function…

Mark McCurry

SoftSeven