Merge two arrays?

B

Brian Mitchell

What's the easiest way to merge two arrays?

It really depends on what you mean by merge.

You could do stuff like:

[1,2] + [2,3] #=3D> [1,2,2,3]
[1,2] | [2,3] #=3D> [1,2,3]

Some motivation for the question might help use in giving you good pointers=
 
J

Joshua Muheim

Joshua said:
This already anwers my question, thank you. :)

Hrm oke, got another related question.

array1 = [1,2,4,6]
array2 = [1,3,5,6]

Is there an easy way for doing this?

array1 ?? array2 = [1,6] # Only add entries that occur in both arrays
array1 ?? array2 = [2,3,4,5] # Only add entries that occur in one of
both arrays
 
B

Brian Mitchell

array1 =3D [1,2,4,6]
array2 =3D [1,3,5,6]

Is there an easy way for doing this?

array1 ?? array2 =3D [1,6] # Only add entries that occur in both arrays
array1 ?? array2 =3D [2,3,4,5] # Only add entries that occur in one of
both arrays

array1 & array2 #=3D> [1,6]

the second is harder but you could do it like:
(array1 + array2) - (array1 & array2) #=3D> [2,3,4,5]

I am not sure if that can be made simpler but it should work.

Brian.
 
Y

YANAGAWA Kazuhisa

In Message-Id: <[email protected]>
Joshua Muheim said:
array1 = [1,2,4,6]
array2 = [1,3,5,6]

array1 ?? array2 = [1,6] # Only add entries that occur in both arrays
array1 ?? array2 = [2,3,4,5] # Only add entries that occur in one of
both arrays

For former Array#& can be used if there're no duplicated elements in
your arrays. For latter, hmm, I can't remember no one methods but you
can (array1|array2)-(array1&array2), on same assumption.

Note these operators always make a new array for a result, they may
not be suitable when both your arrays are too large, though
 
J

Joshua Muheim

For former Array#& can be used if there're no duplicated elements in
your arrays.

Hrm I don't really got this... Where do I have to put the references
(array1, array2) for this?
Note these operators always make a new array for a result, they may
not be suitable when both your arrays are too large, though

Thanks for the hint. But they don't create clones of the objects in the
arrays, they just copy the references, right?
 
G

Gene Tani

YANAGAWA said:
In Message-Id: <[email protected]>
Joshua Muheim said:
array1 = [1,2,4,6]
array2 = [1,3,5,6]

array1 ?? array2 = [1,6] # Only add entries that occur in both arrays
array1 ?? array2 = [2,3,4,5] # Only add entries that occur in one of
both arrays

For former Array#& can be used if there're no duplicated elements in
your arrays. For latter, hmm, I can't remember no one methods but you
can (array1|array2)-(array1&array2), on same assumption.

Note these operators always make a new array for a result, they may
not be suitable when both your arrays are too large, though


--

be careful with ''&' and '-'
http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/65361
 
Y

YANAGAWA Kazuhisa

In Message-Id: <[email protected]>
Joshua Muheim said:
Hrm I don't really got this... Where do I have to put the references
(array1, array2) for this?

That's already answered by others so I don't repeat....

Thanks for the hint. But they don't create clones of the objects in the
arrays, they just copy the references, right?

That's right. By above saying I want to note on memory utilization
issue:

Since very_huge_array (operator) another_very_huge_array possibly
yields yet_another_very_huge_array, (a1|a2)-(a1&a2) can creates two
huge arrays which immediately discarded after the computation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top