Posted 17 January 2013 - 05:23 AM
Urrrgh, I'm a bit new to LUA and I need some hint on how to tackle an issue I've got.
I want to keep a table that holds details of monitored turtles (current position, status, fuel, etc) - this is to run on a console, so that a single console can be used to monitor multiple turtles over rednet. The operator can only see the details of one turtle at a time, but the program will be monitoring all rednet messages and storing the latest set of details for each turtle into a table - the key being the turtle ID, the value being another table itself with various key/value pairs in it.
The problem I have is trying to think how I can efficiently add a turtle entry into the table when a new message from a never-heard-from-before turtle hits the console.
Now, the ideal thing would be something like LUA's table.insert - but this can only insert/append elements into the table on a numerical key basis. So 1,2,3,4 etc. My turtle IDs could arrive in completely random order and will rarely be sequential. 33, 38, 15, etc.
It seems the only way I can create an entry into a table with a specified non-ordered numeric key (it could be a string, for all intents and purposes) is to create the table with the format :-
…but then I'm faced with the issue how do I add subsequent elements into the table?!
I could receive a message from a turtle, scan the existing table to see if an entry already exists (and if it does, just update it - that's easy enough). If not, I cannot obviously do just the above as it will wipe out the entirety of the existing t. So do I need to loop through it taking a copy of its elements into a temporary table, then add on my new one, then copy it all back into the original table?
My other option is to just use a table indexed numerically and sequentially, using the table.append function to add new items, and putting a key within the subtable with the turtle ID. But that then strikes me as becoming rather inefficient, as I will need to scan the entire table's entries, checking to see if the turtle has already been recorded, before doing the append. And it will be receiving messages quite regularly, so I'd like to make it as efficient as I could.
I guess the crux of the question is….
Is there a better method to insert a new k/v pair into an existing table where the keys are all non-sequential integers (or even strings)?
Apologies if this turns out to be a real example of n00bishness - I did search, honest, and I've googled, but not found anything obvious yet bar my approach above - if that's the way it needs to be done, then fair enough, but figured there might be a nicer way. Damn it, table.append is soooo close to what I need :)/> !!
edit: Or would it be best to just insert the numeric keys with nil values in the gaps? So if I have the highest current recorded turtle as 33, and a message from 38 comes in, just do 4 inserts with no values, and then the 5th insert can set the correct key entry. Obviously some minor memory overheard on the entire table but is this least of my worries if efficiency is my goal?
I want to keep a table that holds details of monitored turtles (current position, status, fuel, etc) - this is to run on a console, so that a single console can be used to monitor multiple turtles over rednet. The operator can only see the details of one turtle at a time, but the program will be monitoring all rednet messages and storing the latest set of details for each turtle into a table - the key being the turtle ID, the value being another table itself with various key/value pairs in it.
The problem I have is trying to think how I can efficiently add a turtle entry into the table when a new message from a never-heard-from-before turtle hits the console.
Now, the ideal thing would be something like LUA's table.insert - but this can only insert/append elements into the table on a numerical key basis. So 1,2,3,4 etc. My turtle IDs could arrive in completely random order and will rarely be sequential. 33, 38, 15, etc.
It seems the only way I can create an entry into a table with a specified non-ordered numeric key (it could be a string, for all intents and purposes) is to create the table with the format :-
t = { [20] = tmpSubtable }
…but then I'm faced with the issue how do I add subsequent elements into the table?!
I could receive a message from a turtle, scan the existing table to see if an entry already exists (and if it does, just update it - that's easy enough). If not, I cannot obviously do just the above as it will wipe out the entirety of the existing t. So do I need to loop through it taking a copy of its elements into a temporary table, then add on my new one, then copy it all back into the original table?
My other option is to just use a table indexed numerically and sequentially, using the table.append function to add new items, and putting a key within the subtable with the turtle ID. But that then strikes me as becoming rather inefficient, as I will need to scan the entire table's entries, checking to see if the turtle has already been recorded, before doing the append. And it will be receiving messages quite regularly, so I'd like to make it as efficient as I could.
I guess the crux of the question is….
Is there a better method to insert a new k/v pair into an existing table where the keys are all non-sequential integers (or even strings)?
Apologies if this turns out to be a real example of n00bishness - I did search, honest, and I've googled, but not found anything obvious yet bar my approach above - if that's the way it needs to be done, then fair enough, but figured there might be a nicer way. Damn it, table.append is soooo close to what I need :)/> !!
edit: Or would it be best to just insert the numeric keys with nil values in the gaps? So if I have the highest current recorded turtle as 33, and a message from 38 comes in, just do 4 inserts with no values, and then the 5th insert can set the correct key entry. Obviously some minor memory overheard on the entire table but is this least of my worries if efficiency is my goal?