Combining 2D Arrays?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Combining 2D Arrays?

Post by Davidobot »

How could one combine two 2D arrays?
For example this:

Code: Select all

map = {
	        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 1 },
	        { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
	        { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
	        { 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
	        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	        { 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
	        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	    }
And this:

Code: Select all

map = {
	        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 1 },
	        { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
	        { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
	        { 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
	        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
	        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
	        { 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
	        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
	    }
So that it becomes on table. Combining them next to each other.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
MPQC
Citizen
Posts: 65
Joined: Fri Jun 28, 2013 2:45 pm

Re: Combining 2D Arrays?

Post by MPQC »

Could you give an example of what you mean by "combining them next to each other"? I have a feeling if you read this thread it'll answer your question though.

*Edit: Might have misunderstood. Thought they were strings.
Last edited by MPQC on Wed Jul 31, 2013 8:29 pm, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Combining 2D Arrays?

Post by micha »

A 2d-array is a table of tables. So you should first try to figure out how to concatenate (combinate) two tables: Lets say you have

Code: Select all

a = {1,2,3}
b = {5,6,7}
Try to write a function that generates a table

Code: Select all

{1,2,3,4,5,6}
from this.

Once you have this function you can use it to concatenate 2d-arrays. If you want to have them side-by-side then you need a for loop which loops over the rows and for each row combines the two parts from the individual arrays. If you want to the two arrays on top of each other, then instead you do not need a loop but can directly combine the two (outer) tables.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Combining 2D Arrays?

Post by Ranguna259 »

if what you want is what mich said then:

Code: Select all

a = {1,2,3}
b = {5,6,7}
Then this should do the trick:

Code: Select all

for i=1,#b do
table.insert(a,b[i])
end
I'll code a for loop for 2d arrays, just wait a few mins.

EDIT:
This should do the trick(it'll concatenate table a with b):

Code: Select all

a = {{1,2,3},
	 {8,9,10},
	 {14,15,16}}
b = {{5,6,7},
	 {11,12,13},
	 {17,18,19}}


for i=1,#b do
	 for ii=1, #b[i] do
		 table.insert(a[i],b[i][ii])
	 end
end
--debugger
for i=1,#a do
	 print(unpack(a[i]))
end
Test it out and post back ^^
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Combining 2D Arrays?

Post by Jasoco »

Are you trying to make the two 2D tables into a single 3D table or one single double-wide 2D table with table A on the left and table B on the right?

I was assuming the 3D one. (Where they are on top of each other. So basically the top-left cell for your new table would be the values of table 1 and 2. Basically x:1, y:1 would equal the values of cell 1, 1 from each, or {1, 1}.)
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Combining 2D Arrays?

Post by Davidobot »

Jasoco wrote:Are you trying to make the two 2D tables into a single 3D table or one single double-wide 2D table with table A on the left and table B on the right?

I was assuming the 3D one. (Where they are on top of each other. So basically the top-left cell for your new table would be the values of table 1 and 2. Basically x:1, y:1 would equal the values of cell 1, 1 from each, or {1, 1}.)
I am trying to make them into one single double-wide 2D table with table A on the left and table B on the right.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Combining 2D Arrays?

Post by Ranguna259 »

Davidobot wrote:
Jasoco wrote:Are you trying to make the two 2D tables into a single 3D table or one single double-wide 2D table with table A on the left and table B on the right?

I was assuming the 3D one. (Where they are on top of each other. So basically the top-left cell for your new table would be the values of table 1 and 2. Basically x:1, y:1 would equal the values of cell 1, 1 from each, or {1, 1}.)
I am trying to make them into one single double-wide 2D table with table A on the left and table B on the right.
Yep that's what my for loop does.

EDIT:
Here's your unpacked table:

Code: Select all

1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1
1	0	0	0	0	0	0	0	1	0	0	0	4	1	1	0	0	0	0	0	0	0	1	0	0	0	4	1
1	0	1	1	0	0	0	0	1	0	0	1	0	1	1	0	1	1	0	0	0	0	1	0	0	1	0	1
1	0	1	1	0	0	0	0	1	0	0	1	0	1	1	0	1	1	0	0	0	0	1	0	0	1	0	1
1	0	0	1	0	1	1	1	1	0	0	0	0	1	1	0	0	1	0	1	1	1	1	0	0	0	0	1
1	0	0	1	0	0	0	0	1	0	0	0	0	1	1	0	0	1	0	0	0	0	1	0	0	0	0	1
1	0	0	1	0	0	0	0	1	0	0	0	0	1	1	0	0	1	0	0	0	0	1	0	0	0	0	1
1	0	0	1	0	0	0	0	1	0	0	0	0	1	1	0	0	1	0	0	0	0	1	0	0	0	0	1
1	0	0	1	0	0	0	0	0	0	0	0	0	1	1	0	0	1	0	0	0	0	0	0	0	0	0	1
1	0	0	0	0	0	0	0	1	1	1	1	0	1	1	0	0	0	0	0	0	0	1	1	1	1	0	1
1	0	0	0	0	0	0	0	1	0	0	0	0	1	1	0	0	0	0	0	0	0	1	0	0	0	0	1
1	2	0	0	0	0	0	0	0	0	0	0	0	1	1	2	0	0	0	0	0	0	0	0	0	0	0	1
1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1
(EDIT2:Huh.. there's a one missing)
(EDIT3:lol missed it when I was copying :rofl: fixed )

And here's the code:

Code: Select all

mapa = {
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
       }
mapb = {
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
       }


for i=1,#mapb do
	 for ii=1, #mapb[i] do
		 table.insert(mapa[i],mapb[i][ii])
	 end
end
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Combining 2D Arrays?

Post by Davidobot »

Thank you! :D
Works perfectly! Infinite worlds, here I come! :joker:
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Combining 2D Arrays?

Post by Ranguna259 »

Davidobot wrote:Thank you! :D
Works perfectly! Infinite worlds, here I come! :joker:
infinite :ultrashocked: well.. don't forget to optimize it so it will discard maps as you move forward or backward and reload them once you get close to them again because if you don't there'll be a massive impact on the performance ;)
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Combining 2D Arrays?

Post by Davidobot »

Ranguna259 wrote:
Davidobot wrote:Thank you! :D
Works perfectly! Infinite worlds, here I come! :joker:
infinite :ultrashocked: well.. don't forget to optimize it so it will discard maps as you move forward or backward and reload them once you get close to them again because if you don't there'll be a massive impact on the performance ;)
Now that is another problem, I will now need a remove function?! :shock:
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
Post Reply

Who is online

Users browsing this forum: No registered users and 224 guests