[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Is there a quick way to compare two arrays and ....

Victor Reyes

1/9/2006 8:54:00 PM

I have two arrays of different sizes.
Right now via find_all I can find all the elements that are common to both
arrays.
All the elements in the smaller arrays can be found in the larger array.

What I would like to find now is all th elements that are in the larger
array and missing on the smaller array.
I know I can iterate and compare each element within a nested loop.
But is there a way to print out the elements that are missing from the
smaller array without having to implement a nested loop?

Thank you

Victor
3 Answers

Ezra Zygmuntowicz

1/9/2006 9:02:00 PM

0


On Jan 9, 2006, at 12:53 PM, Victor Reyes wrote:

> I have two arrays of different sizes.
> Right now via find_all I can find all the elements that are common
> to both
> arrays.
> All the elements in the smaller arrays can be found in the larger
> array.
>
> What I would like to find now is all th elements that are in the
> larger
> array and missing on the smaller array.
> I know I can iterate and compare each element within a nested loop.
> But is there a way to print out the elements that are missing from the
> smaller array without having to implement a nested loop?
>
> Thank you
>
> Victor

Like this?

irb(main):009:0> a = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):010:0> b = [1,2,3]
=> [1, 2, 3]
irb(main):011:0> a - b
=> [4, 5]

Cheers-
-Ezra



Victor Reyes

1/10/2006 1:38:00 PM

0

Ezra,
Thanks a bunch.
This is soooooooooo simple that it is laughable.
I guess I need to read my Ruby books a bit more.

Thanks again

On 1/9/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:
>
>
> On Jan 9, 2006, at 12:53 PM, Victor Reyes wrote:
>
> > I have two arrays of different sizes.
> > Right now via find_all I can find all the elements that are common
> > to both
> > arrays.
> > All the elements in the smaller arrays can be found in the larger
> > array.
> >
> > What I would like to find now is all th elements that are in the
> > larger
> > array and missing on the smaller array.
> > I know I can iterate and compare each element within a nested loop.
> > But is there a way to print out the elements that are missing from the
> > smaller array without having to implement a nested loop?
> >
> > Thank you
> >
> > Victor
>
> Like this?
>
> irb(main):009:0> a = [1,2,3,4,5]
> => [1, 2, 3, 4, 5]
> irb(main):010:0> b = [1,2,3]
> => [1, 2, 3]
> irb(main):011:0> a - b
> => [4, 5]
>
> Cheers-
> -Ezra
>
>
>

David Vallner

1/11/2006 5:46:00 AM

0

Shame not, young grasshopper, "substracting" arrays only really makes
perfect sense after you know it's there. Isn't there a verbose name for
the method? I know Java had a removeAll(Collection) method on one of the
collection interfaces since gods know when, so it's not really that
Obscure Voodoo (tm).

David Vallner

Victor Reyes wrote:

>Ezra,
>Thanks a bunch.
>This is soooooooooo simple that it is laughable.
>I guess I need to read my Ruby books a bit more.
>
>Thanks again
>
>On 1/9/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:
>
>
>>On Jan 9, 2006, at 12:53 PM, Victor Reyes wrote:
>>
>>
>>
>>>I have two arrays of different sizes.
>>>Right now via find_all I can find all the elements that are common
>>>to both
>>>arrays.
>>>All the elements in the smaller arrays can be found in the larger
>>>array.
>>>
>>>What I would like to find now is all th elements that are in the
>>>larger
>>>array and missing on the smaller array.
>>>I know I can iterate and compare each element within a nested loop.
>>>But is there a way to print out the elements that are missing from the
>>>smaller array without having to implement a nested loop?
>>>
>>>Thank you
>>>
>>>Victor
>>>
>>>
>>Like this?
>>
>>irb(main):009:0> a = [1,2,3,4,5]
>>=> [1, 2, 3, 4, 5]
>>irb(main):010:0> b = [1,2,3]
>>=> [1, 2, 3]
>>irb(main):011:0> a - b
>>=> [4, 5]
>>
>>Cheers-
>>-Ezra
>>
>>
>>
>>
>>
>
>
>