[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Merge two arrays?

Joshua Muheim

1/12/2006 10:18:00 AM

Hi all

What's the easiest way to merge two arrays?

Thanks a lot,
Josh

--
Posted via http://www.ruby-....


11 Answers

Brian Mitchell

1/12/2006 10:27:00 AM

0

On 1/12/06, Joshua Muheim <forum@josh.ch> wrote:
> 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] #=> [1,2,2,3]
[1,2] | [2,3] #=> [1,2,3]

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

Joshua Muheim

1/12/2006 10:32:00 AM

0

This already anwers my question, thank you. :-)

--
Posted via http://www.ruby-....


Joshua Muheim

1/12/2006 10:45:00 AM

0

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


Brian Mitchell

1/12/2006 11:12:00 AM

0

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

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.


Joshua Muheim

1/12/2006 12:02:00 PM

0

Thanks a lot. :-)

--
Posted via http://www.ruby-....


YANAGAWA Kazuhisa

1/12/2006 12:12:00 PM

0

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.



Joshua Muheim

1/12/2006 12:20:00 PM

0

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


Gene Tani

1/12/2006 4:01:00 PM

0


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 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-...
> kjana@dm4lab.to January 12, 2006
> Time and tide wait for no man.

MenTaLguY

1/12/2006 4:55:00 PM

0

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


YANAGAWA Kazuhisa

1/12/2006 11:40:00 PM

0

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


--
kjana@dm4lab.to January 13, 2006
Behind the clouds is the sun still shining.