Jump to content

Table.insert top or bottom of the table?


ORMtnMan

Recommended Posts

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 end
t[#t] = nil -- Remove last entry in the table
That's because I usually iterate over sequences using the '#' operator:

local foo = { 'a', 'b', 'c' } for i = 1, #foo do    print(foo[i])end
This 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

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 end
t[#t] = nil -- Remove last entry in the table
That's because I usually iterate over sequences using the '#' operator:

local foo = { 'a', 'b', 'c' } for i = 1, #foo do    print(foo[i])end
This 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...