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

V

Victor Reyes

------=_Part_41428_24833639.1136840004608
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

------=_Part_41428_24833639.1136840004608--
 
E

Ezra Zygmuntowicz

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
 
V

Victor Reyes

------=_Part_6187_8487294.1136900293733
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

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 =3D [1,2,3,4,5]
=3D> [1, 2, 3, 4, 5]
irb(main):010:0> b =3D [1,2,3]
=3D> [1, 2, 3]
irb(main):011:0> a - b
=3D> [4, 5]

Cheers-
-Ezra

------=_Part_6187_8487294.1136900293733--
 
D

David Vallner

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 said:
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

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
 

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
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top