CarlosMX
|
  |
| Joined: 30 Apr 2011 |
| Total Posts: 791 |
|
|
| 15 Mar 2017 05:14 PM |
This has me stumped... say a string is already inserted into a table (table.insert(Table, 1, "Person")... and assigned it's value inside of the table (Table[1]), is there a way to move that "Person" to another area in the table?
Table = {}
table.insert(Table, 1, "Person1") table.insert(Table, 2, "Person2")
And now you want to assign "Person1" to value "2" inside of the table... is there a way of doing this? Or would you just have to locate "Person1" inside of the table, remove them, then reinsert them into the table with the value? |
|
|
| Report Abuse |
|
|
|
| 15 Mar 2017 05:15 PM |
Table[1], Table[2] = Table[2], Table[1]
|
|
|
| Report Abuse |
|
|
CarlosMX
|
  |
| Joined: 30 Apr 2011 |
| Total Posts: 791 |
|
| |
|
|
| 15 Mar 2017 05:17 PM |
local tbl = {"1", "2", "3"}
tbl[1] = 3 tbl[3] = 1
print(tbl[1]) print(tbl[3]) |
|
|
| Report Abuse |
|
|
|
| 15 Mar 2017 05:18 PM |
Table[DesiredValue] = Table[CurrentValue]; table.remove(Table, CurrentValue); --if you're not assigning keys to the table Table[CurrentValue] = nil --if you are assigning keys to the value and aren't using consecutive numerals.
18,821 |
|
|
| Report Abuse |
|
|
|
| 15 Mar 2017 05:20 PM |
you could also copy it to another position
Table[DesiredValue] = Table[CurrentValue];
18,821 |
|
|
| Report Abuse |
|
|
CarlosMX
|
  |
| Joined: 30 Apr 2011 |
| Total Posts: 791 |
|
|
| 15 Mar 2017 05:20 PM |
Wait hang on... I'm like literally trying to reposition the value, such as if you were to insert a new string to the table it moves the rest of the elements down if you were to put it in front of them...
table.insert(Table, 2, "Person4") < would move Person2 who was originally here down to Table[3].. I'm not trying to reassign the value of the number... |
|
|
| Report Abuse |
|
|
|
| 15 Mar 2017 05:22 PM |
The table.insert() and table.remove() functions automatically prevent holes in arrays. It should work fine.
|
|
|
| Report Abuse |
|
|
|
| 15 Mar 2017 05:22 PM |
local value = table[CurrentPosition]; table.remove(table, CurrentPosition); table.insert(table, DesiredPosition, value);
18,821 |
|
|
| Report Abuse |
|
|
CarlosMX
|
  |
| Joined: 30 Apr 2011 |
| Total Posts: 791 |
|
| |
|