Merge two arrays

Hi,

[b]is there any method to merge one array values in to another array, i mean i have a different situation here. There are 2 instance variables and generating output like this :[/b] [code=]@users_count = [["Feb 2008", 1], ["Mar 2008", 3], ["Apr 2008", 5], ["May 2008", 5], ["Jun 2008", 3], ["Jul 2008", 2], ["Aug 2008", 1]] @customers_count = [["Mar 2008", 6], ["Apr 2008", 8], ["May 2008", 6], ["Jun 2008", 9]]

i want to merge one array in to another to look like : @chart_data = [["Feb 2008", 1], ["Mar 2008", 3, 6], ["Apr 2008", 5, 8], ["May 2008", 5, 6], ["Jun 2008", 3, 9], ["Jul 2008", 2], ["Aug 2008", 1]][/code] [b] this type of data is required to pass and display for a fusion chart representing 2 different lines with values on chart and month names on bottom, i know this would be a strange case....[/b]

any help ?? thanks

I think the method you're looking for is zip.

@users_count.zip(@customers_count)

Hi --

Hi,

[b]is there any method to merge one array values in to another array, i mean i have a different situation here. There are 2 instance variables and generating output like this :[/b] [code=]@users_count = [["Feb 2008", 1], ["Mar 2008", 3], ["Apr 2008", 5], ["May 2008", 5], ["Jun 2008", 3], ["Jul 2008", 2], ["Aug 2008", 1]] @customers_count = [["Mar 2008", 6], ["Apr 2008", 8], ["May 2008", 6], ["Jun 2008", 9]]

i want to merge one array in to another to look like : @chart_data = [["Feb 2008", 1], ["Mar 2008", 3, 6], ["Apr 2008", 5, 8], ["May 2008", 5, 6], ["Jun 2008", 3, 9], ["Jul 2008", 2], ["Aug 2008", 1]][/code] [b] this type of data is required to pass and display for a fusion chart representing 2 different lines with values on chart and month names on bottom, i know this would be a strange case....[/b]

any help ??

One possibility:

def merge_two(a,b)    [ a[0], *[a[-1],b[-1]].sort ] end

res = @users_count.map do |uc|    if cc = @customers_count.find {|c| c[0] == uc[0] }      merge_two(uc,cc)    else      uc    end end

p res

David

Hi --

David A. Black wrote:

Hi --

5], ["May 2008", 5], ["Jun 2008", 3], ["Jul 2008", 2], ["Aug 2008", this type of data is required to pass and display for a fusion chart representing 2 different lines with values on chart and month names on bottom, i know this would be a strange case....[/b]

any help ?? thanks

I think the method you're looking for is zip.

@users_count.zip(@customers_count)

No, that doesn't produce the desired result.

David

-- Rails training from David A. Black and Ruby Power and Light:   * Advancing With Rails August 18-21 Edison, NJ   * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates!

Hi David, yep you are right !! Great and Thanks a lot for Replying, it works fantastic in my application and what about if we have third variable say @products_count, can we merge this too

Hi --

I would just use the union method on the Array class: http://www.ruby-doc.org/core/classes/Array.html#M000325

users_count = [["Feb 2008", 1], ["Mar 2008", 3], ["Apr 2008", 5], ["May 2008", 5], ["Jun 2008", 3], ["Jul 2008", 2], ["Aug 2008", 1]]

=> [["Feb 2008", 1], ["Mar 2008", 3], ["Apr 2008", 5], ["May 2008", 5], ["Jun 2008", 3], ["Jul 2008", 2], ["Aug 2008", 1]]

customers_count = [["Mar 2008", 6], ["Apr 2008", 8], ["May 2008", 6], ["Jun 2008", 9]]

=> [["Mar 2008", 6], ["Apr 2008", 8], ["May 2008", 6], ["Jun 2008", 9]]

chart_data = users_count | customers_count

=> [["Feb 2008", 1], ["Mar 2008", 3], ["Apr 2008", 5], ["May 2008", 5], ["Jun 2008", 3], ["Jul 2008", 2], ["Aug 2008", 1], ["Mar 2008", 6], ["Apr 2008", 8], ["May 2008", 6], ["Jun 2008", 9]]

-Harold

Why not use two arrays of hashes?

@users_count = [ { :month => “Feb 2008”, :count => 1}, { :month => “Mar 2008”, :count => 3},

{ :month => “Apr 2008”, :count => 5}, { :month => “May 2008”, :count => 5}, { :month => “Jun 2008”, :count => 3}, { :month => “Jul 2008”, :count => 2},

{ :month => “Aug 2008”, :count => 1} ] @customers_count = [ { :month => “Mar 2008”, :count => 6}, { :month => “Apr 2008”, :count => 8}, { :month => “May 2008”, :count => 6},

{ :month => “Jun 2008”, :count => 9}, ]

and perform your merge like this:

@chart_data = @users_count.each_with_index do |i,hash| @chart_data << [hash[:month],hash[:count],@customers_count[i][:count]]

end

Hi --

Definitely...I misread the original post. Apologies.

Hi --

David A. Black wrote:

Hi --

def merge_two(a,b)

p res

Hi, i was not getting the remaining values in @customers_count as it was merging the matched ones and displaying with the remaining values in @users_count..

I'm afraid I'm not sure what you mean.

David

-- Rails training from David A. Black and Ruby Power and Light:   * Advancing With Rails August 18-21 Edison, NJ   * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates!

@users_count = [["Feb 2008", 1], ["Mar 2008", 3], ["Apr 2008", 5], ["May 2008", 5], ["Jun 2008", 3], ["Aug 2008", 1]] @customers_count = [["Mar 2008", 6], ["Apr 2008", 8], ["May 2008", 6], ["Jun 2008", 9]], ["Jul 2008", 2] the output i am getting is res = [["Feb 2008", 1], ["Mar 2008", 3, 6], ["Apr 2008", 5, 8], ["May 2008", 5, 6], ["Jun 2008", 3, 9], ["Aug 2008",1]]

but i am missing ["Jul 2008", 2] which was in @customers_count as the else part is displaying remaining values for uc only but not the remaining values in cc

Sorry, if i was wrong .I am a newbie in this..

thanks