ORMtnMan Posted September 8, 2015 Share Posted September 8, 2015 Hey everyone, Quick question, when using table.insert does it insert the new entry at the top or bottom of the table index wise? Thanks! Link to comment Share on other sites More sharing options...
RoboMat Posted September 8, 2015 Share Posted September 8, 2015 table.insert and table.remove both have optional index paramters which allow you to specify at which position to insert / remove a value. Without them, the functions will always operate on the end of the table. table.insert({ 'b', 'c' }, 1, 'a') table.insert would insert 'a' at the first position in the table and automatically shifts all the other values down accordingly. table.remove({ 'a', 'b', 'c' }, 1) table.remove would remove the first entry and shift the others down in this example. The important thing to keep in mind is that this only works on sequences:We use the term sequence to denote a table where the set of all positive numeric keys is equal to {1..n} for some integer n, which is called the length of the sequence (see §3.4.6).A small sidenote. If you have a sequence and want to add / remove entries at the end, I prefer this: t[#t + 1] = 'bar' -- Insert new entry at the endt[#t] = nil -- Remove last entry in the tableThat's because I usually iterate over sequences using the '#' operator:local foo = { 'a', 'b', 'c' } for i = 1, #foo do print(foo[i])endThis should be faster than using table.remove / table.insert and ipairs / pairs, but for me it's just personal preference. Link to comment Share on other sites More sharing options...
ORMtnMan Posted September 9, 2015 Author Share Posted September 9, 2015 table.insert and table.remove both have optional index paramters which allow you to specify at which position to insert / remove a value. Without them, the functions will always operate on the end of the table. table.insert({ 'b', 'c' }, 1, 'a') table.insert would insert 'a' at the first position in the table and automatically shifts all the other values down accordingly. table.remove({ 'a', 'b', 'c' }, 1) table.remove would remove the first entry and shift the others down in this example. The important thing to keep in mind is that this only works on sequences:We use the term sequence to denote a table where the set of all positive numeric keys is equal to {1..n} for some integer n, which is called the length of the sequence (see §3.4.6).A small sidenote. If you have a sequence and want to add / remove entries at the end, I prefer this: t[#t + 1] = 'bar' -- Insert new entry at the endt[#t] = nil -- Remove last entry in the tableThat's because I usually iterate over sequences using the '#' operator:local foo = { 'a', 'b', 'c' } for i = 1, #foo do print(foo[i])endThis should be faster than using table.remove / table.insert and ipairs / pairs, but for me it's just personal preference. RoboMat, you are a scholar and a gentleman! This perfectly answers my question! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now