Merge two arrays

Hi,

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 :

[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]

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…

any help ??
thanks

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

@users_count.zip(@customers_count)

Hi –

On Fri, 1 Aug 2008, Srinath A. wrote:

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


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 –

On Fri, 1 Aug 2008, Ryan B. wrote:

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 –

On Fri, 1 Aug 2008, Srinath A. wrote:

bottom, i know this would be a strange case…[/b]

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

Of course. But I shall leave that as an exercise to the reader :slight_smile: The
techniques you need should be present in the two-array example.

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!

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

David A. Black wrote:

Hi –

On Fri, 1 Aug 2008, Ryan B. wrote:

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 –

On Fri, 1 Aug 2008, Harold wrote:

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]]

That’s not the result that was being sought, though.

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!

Definitely…I misread the original post. Apologies.

On Fri, Aug 1, 2008 at 6:23 AM, Srinath A.
<[email protected]

wrote:

[“May 2008”, 5, 6], [“Jun 2008”, 3, 9], [“Jul 2008”, 2], [“Aug 2008”,
1]][/code]

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…

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


Tim

David A. Black wrote:

Hi –

On Fri, 1 Aug 2008, Srinath A. wrote:

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


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,
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

thanks

Hi –

On Mon, 4 Aug 2008, Srinath A. wrote:

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!

David A. Black wrote:

Hi –

On Mon, 4 Aug 2008, Srinath A. wrote:

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