From: Martin DeMello Date: 2012-05-02T12:00:33+09:00 Subject: Re: Overwriting one Ruby array or arrays with another Can you change your underlying data structure? Most efficient would be to let the data be indexed by the (x,y) coordinates (either using a two dimensional array, or a hash of {[x, y] => value}). If you have to keep the list of [[x, y, value]] representation, you could run a merge sort on the two lists, and apply the overlay if you hit cells with equal x and y values. This assumes that both lists are sorted, of course. martin On Tue, May 1, 2012 at 7:50 PM, Craig Law wrote: > Hi > > Hoping someone can give me some direction with regards to overwriting > one Ruby array of arrays with the contents of another. > > As an example I have an array of arrays ... > > [[1, 1, "X"], [1, 2, "X"], [1, 3, "X"], [2, 1, "X"], [2, 2, "X"], [2, 3, > "X"], [3, 1, "X"], [3, 2, "X"], [3, 3, "X"]] > > The first two values of the array represent x, y coordinates to build a > 3 x 3 grid. > > I have another array of arrays ... > > [[1, 1, "O"], [2, 1, "O"], [2, 2, "O"], [2, 3, "O"], [3, 3, "O"]] > > ... which I'd like to merge/overwrite/whatever based ONLY on the x, y > coordinates which will produce the final array (still a 3 x 3 grid) ... > > [[1, 1, "O"], [1, 2, "X"], [1, 3, "X"], [2, 1, "O"], [2, 2, "O"], [2, 3, > "O"], [3, 1, "X"], [3, 2, "X"], [3, 3, "O"]] > > This is just an example and my project will be working with grids that > may be as large as 50 x 100 and the array of arrays that will overwrite > might have 1000+ items. > > Can anyone point me in the right direction as to what would be the most > efficient way of achieving this? > > Many thanks > > Craig > > -- > Posted via http://www.ruby-forum.com/. >