Table usage question

I am not sure what the problem is…maybe the IDE, most likely me though !!

The issue I am having is with a table in the agent that I am using to save parameters to the server so that these can be preserved through a power outage, much as shown in the http://electricimp.com/docs/resources/permanentstore/ article, with parameters being obtained using http, as shown in Micro Disco Light show example.

It was working upto a point with 3 defined integer values in the table, and then I tried to add a further key to the table, a string for email address and I got “Index xxx does not exist” when trying to send that parameter by http.

I thought maybe that you cannot mix integer and strings in a table, so I made the integers into strings so that all keys would be of the same type… that didn’t help.

I even cut and pasted existing code for one of the key values and renamed the variables to add another key to the same table, but that didn’t work either.

Is there possibly some problem with adding keys to a table after it has initially been defined, not in real time, but in subsequent builds?

I defined a fresh table in which to store just the email address, and that worked…of course, it doesn’t solve my problem as only one table can be preserved with server.save()/server.load() technique.

My only thought is to delete the table, and assign a new one with all the keys defined, rename the variables to suit, plus include a few spares for future expansion…

Phil

You can definitely add more fields to the table, and they can be of different types.
Can you provide any code as an example?

You need to use the <- operator to create a slot in a table, adn the = operator to reassign an existing value in the table.

A typical flow that I use looks like this:

// 1) load saved data // 2) check if field exists and create if necessary // 3) Assign value we want to store // 4) Save data local data = server.load(); if (!("someField" in data)) { data["someField"] <- null; } data["someField"] = value; server.save(data);

Thanks.

I think I fixed my problem by clearing the server-stored table, manually as it were, whenever I add another key in my program. I think this has to be done.
Of course, with a bit of planning, I could have set the desired number of keys to start with!
Also, I was refreshing the stored values continually, rather than as an initial action…oops, and there was a typo!!