Thread Tools Display Modes
07-26-09, 06:18 PM   #1
Psychophan7
A Chromatic Dragonspawn
Join Date: Feb 2006
Posts: 153
Concatenating two tables together?

Let's pretend I have two tables:
one = { 1="a", 2="c", 3="e" }
two = { 1="b", 2="d", 3="f" }

And what I want to do with these tables is print the values like so:
ab
cd
ef

I want to take the nth value from table one and then takes the nth value from table two. What can be done to do this?

Alternatively, is there a way to have two (or more) values per index in a table? Following the pattern of index, value1, value2:
1, a, b
2, c, d
3, e, f
  Reply With Quote
07-26-09, 06:25 PM   #2
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Tables can have other tables inside of them i.e.
Code:
local myTable = {
    {"a", "c"},
    {"b", "e"},
    {"d", "f"}
}
Example 1: print(myTable[1][2]) --prints "c"

Example 2:
Code:
for i = 1, #myTable do
   print(myTable[i][1]..myTable[i][2])
end
will print:
ac
be
df
  Reply With Quote
07-26-09, 06:28 PM   #3
jaliborc
A Chromatic Dragonspawn
 
jaliborc's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 196
Originally Posted by Psychophan7 View Post
Let's pretend I have two tables:
one = { 1="a", 2="c", 3="e" }
two = { 1="b", 2="d", 3="f" }

And what I want to do with these tables is print the values like so:
ab
cd
ef
Code:
for i,v in pairs(one) do
      print(v .. two[i])
end
  Reply With Quote
08-03-09, 04:02 AM   #4
Psychophan7
A Chromatic Dragonspawn
Join Date: Feb 2006
Posts: 153
Originally Posted by Akryn View Post
Example 2:
Code:
for i = 1, #myTable do
   print(myTable[i][1]..myTable[i][2])
end
Question about something on the first line: #myTable
I put =#myTable into the interpreter and it gives 3. I remove the # and it gives the memory location. What does the # do?
  Reply With Quote
08-03-09, 04:06 AM   #5
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
# returns the size of the table.

It doesn't however take into account if there's a nil entry between 1 and n. You can read more at http://www.wowwiki.com/API_getn
__________________
Oh, the simulated horror!
  Reply With Quote
08-03-09, 04:59 AM   #6
Katae
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Jun 2007
Posts: 208
I'm late to the thread, but this would be another way to do it...

local t1 = {"a","c","e","g"}
local t2 = {"b","d","f"}
table.foreach(t1, function(i,v) print(i,v,t2[i]) end)

1, a, b
2, c, d
3, e, f
4, g, nil

Last edited by Katae : 08-03-09 at 05:05 AM.
  Reply With Quote
08-03-09, 05:14 AM   #7
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Katae View Post
I'm late to the thread, but this would be another way to do it...

local t1 = {"a","c","e","g"}
local t2 = {"b","d","f"}
table.foreach(t1, function(i,v) print(i,v,t2[i]) end)

1, a, b
2, c, d
3, e, f
4, g, nil
table.foreach is deprecated, don't use it, instead use pairs().
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Concatenating two tables together?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off