I have and array of records that looks like this:
nodes = array()
nodes[index]={“number”:node_number,“last_record”:0,“first_record”:0,“cum_r”:0,“cum_g”:0,“cum_b”:0,“cum_w”:0,“cum_p”:0,“v”:0,“v_ts”:0,“rssi”:0,“status”:0}
to keep track of data on a series of sensor nodes. I run into a problem when I want to add a new node. I can simply do this:
nodes[]={“number”:node_number,“last_record”:0,“first_record”:0,“cum_r”:0,“cum_g”:0,“cum_b”:0,“cum_w”:0,“cum_p”:0,“v”:0,“v_ts”:0,“rssi”:0,“status”:0}
and assign values to the fields. And that works find.
The problem comes in from the fact that the data fields are meant to be dynamic, so that occasionally, I’ll add or delete fields (in all of the array records). But I don’t necessarily track what the most current field configuration is. I thought to simply create a new array element by copying the previous element to give me the current field structure. And then update the fields for the new element to reflect the new data (i.e. below node_number = the new node created ).
nodes[new_index]=nodes[(new_index-1)] //create new element and copy previous
nodes[new_index].number = node_number //update the number field in the new record
So that part almost works, but not according to plan. When I check back on my records, I find that ALL of the records have taken on the new node number that was supposed to be assigned only to nodes[new_index]. Is there something I’m doing wrong that I’m not seeing?
When I update existing records with new values I use the same technique (i.e.: nodes[index].cum_b = value)
which works just fine.