Hi all
What's the easiest way to merge two arrays?
Thanks a lot,
Josh
···
--
Posted via http://www.ruby-forum.com/.
Hi all
What's the easiest way to merge two arrays?
Thanks a lot,
Josh
--
Posted via http://www.ruby-forum.com/.
It really depends on what you mean by merge.
You could do stuff like:
[1,2] + [2,3] #=> [1,2,2,3]
[1,2] | [2,3] #=> [1,2,3]
Some motivation for the question might help use in giving you good pointers.
Brian.
On 1/12/06, Joshua Muheim <forum@josh.ch> wrote:
What's the easiest way to merge two arrays?
This already anwers my question, thank you.
--
Posted via http://www.ruby-forum.com/.
Joshua Muheim wrote:
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
--
Posted via http://www.ruby-forum.com/\.
array1 & array2 #=> [1,6]
the second is harder but you could do it like:
(array1 + array2) - (array1 & array2) #=> [2,3,4,5]
I am not sure if that can be made simpler but it should work.
Brian.
On 1/12/06, Joshua Muheim <forum@josh.ch> wrote:
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
In Message-Id: <7890ecfa1ecf4ba63d489bcce21a1d3e@ruby-forum.com>
Joshua Muheim <forum@josh.ch> writes:
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
--
kjana@dm4lab.to January 12, 2006
Time and tide wait for no man.
Quoting Joshua Muheim <forum@josh.ch>:
Joshua Muheim wrote:
> 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
Here, you're finding the intersection of two sets.
array1 ?? array2 = [2,3,4,5] # Only add entries that occur in one
of both arrays
There are at least two ways to do this:
( array1 | array2 ) - ( array1 & array2 )
or
( array1 - array2 ) | ( array2 - array1 )
Some people call this "exclusion"; it's the inverse of intersection.
However, at this point you might want to consider using Set rather
than Array. Performing set operations on Arrays is not very
efficient.
To use Set, require 'set'. You can create a set like:
Set[1, 3, 5, 6]
or (from an array):
Set[*array]
The set operations union, intersection, and difference (|, &, -)
work the same for sets as they do arrays, and Set also implements
Enumerable (so you get Set#each, Set#to_a, etc...).
Ah, one additional thing -- if you want exclusion, there is a
shorter way to write it with sets:
set1 = Set[1, 2, 4, 6]
set2 = Set[1, 3, 5, 6]
set1 ^ set2 # => #<Set: {5, 2, 3, 4}>
Note that sets are unordered containers.
-mental
Thanks a lot.
--
Posted via http://www.ruby-forum.com/.
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?
--
Posted via http://www.ruby-forum.com/\.
YANAGAWA Kazuhisa wrote:
In Message-Id: <7890ecfa1ecf4ba63d489bcce21a1d3e@ruby-forum.com>
Joshua Muheim <forum@josh.ch> writes:> 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 arraysFor 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
kjana@dm4lab.to January 12, 2006
Time and tide wait for no man.
You're quite welcome.
-mental
On Thu, 2006-01-12 at 18:00 +0100, Joshua Muheim wrote:
Thanks for this detailed explanation.
In Message-Id: <eb9183fe90ad27544becbd573051f512@ruby-forum.com>
Joshua Muheim <forum@josh.ch> writes:
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....
> Note these operators always make a new array for a result, they may
> not be suitable when both your arrays are too large, thoughThanks 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.
--
kjana@dm4lab.to January 13, 2006
Behind the clouds is the sun still shining.