PDA

View Full Version : Table insertion



markstaylor
08-18-2004, 09:02 AM
If I have two tables Table1 and Table2, how can I insert one item of Table2 into Table1 after every 4 items

T1 T2
1 A
2 B
3 C
4 D
... ...
New Table1
1
2
3
4
A
...


Mark

Worm
08-18-2004, 09:41 AM
Someone will probably come up with a more stream-lined way of doing this, but for a quick down and dirty, this should work.



--Initiate tables
tblNum={"1","2","3","4"}
tblAlph={"A","B","C","D"}

--Initiate base variables
blnDone = false
nInsertPos = 5
nCtr = 0

--loop until we say we're done
while blnDone == false do
if Table.Count(tblNum) >= (nInsertPos -1) then
nCtr = nCtr + 1
if Table.Count(tblAlph) >= nCtr then
Table.Insert(tblNum, nInsertPos, tblAlph[nCtr])
nInsertPos = nInsertPos + 5
else
blnDone = true
end
else
blnDone = true
end
end

TJ_Tigger
08-18-2004, 10:17 AM
This should work as well, not quick enough on the draw (code) to beat work. Need more coffee.


--initialize tables
--table 1
table1 = {};
for x = 1,20 do
Table.Insert(table1, x, x);
end

t1display = ""
for y = 1,Table.Count(table1) do
t1display = t1display .." ".. table1[y]
end

Dialog.Message("Table1", t1display)

--table two this uses the ascii decimal values to for A-E
table2 = {};
for x = 65,69 do
Table.Insert(table2, x - 64, String.Char(x));
end

t2display = "";
for t2i, t2v in table2 do
t2display = t2display .." ".. t2v;
end

Dialog.Message("Table2", t2display);

--Check that the tables exist.
if table1 and table2 then
--create a new table and copy the contents from the table1 to the new table
newtable = {};
for index,value in table1 do
Table.Insert(newtable, index, value);
end

--This is what inserts the contents of table2 into the fourth position in the newtable.
for i,v in table2 do
Table.Insert(newtable, (4*i)+i, v)
end

--display for the new table
tndisplay = "";
for tni, tnv in newtable do
tndisplay = tndisplay .." ".. tnv;
end

Dialog.Message("NewTable", tndisplay);
else
Dialog.Message("Notice", "Your tables are not complete. Click the two table buttons to create those tables first");
end


After posting this looks like a lot of code, the code that inserts your information from table two into the new table is this code:


for i,v in table2 do
Table.Insert(newtable, (4*i)+i, v)
end


I hope that helps.

markstaylor
08-18-2004, 11:15 AM
Thats just what I was looking for.

If I create a table from one listbox and a second from another listbox how can I use the example above to fill in the third listbox with the combined data?

Worm
08-18-2004, 11:42 AM
after you get the table set to the values you want, then do something like this.



--clear the listbox
ListBox.DeleteItem("Listbox3", LB_ALLITEMS)

--add the elements from the table to the listbox
for n, Table.Count(tblNum) do
ListBox.AddItem("Listbox3", tblNum[n])
end



Thats just what I was looking for.

If I create a table from one listbox and a second from another listbox how can I use the example above to fill in the third listbox with the combined data?

markstaylor
08-18-2004, 12:56 PM
When I use that example I get compiling error so I changed it to
for n, v in newtable do
ListBox.AddItem("Listbox1", newtable[n]);
end

But I get a "Argument 2 must be of type String" error

Worm
08-18-2004, 01:15 PM
Sorry, I typed that one out. There I go thinking I had the syntax down when I didn't. I didn't have n=1 in the for statement




--clear the listbox
ListBox.DeleteItem("Listbox3", LB_ALLITEMS)

--add the elements from the table to the listbox
for n=1, Table.Count(tblNum) do
ListBox.AddItem("Listbox3", tblNum[n])
end