Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Two arrays: return only values that exist in both arrays?

Reply
Thread Tools

Two arrays: return only values that exist in both arrays?

 
 
Joshua Muheim
Guest
Posts: n/a
 
      11-23-2007
Hi all

a = [ne, :two, :three, :six]
b = [:two, :four, six]

I want to extract the values of two arrays which exist in both ones (a
and b):

# => [:two, :six]

Does there exist a method for this?

Thanks
Josh
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Dejan Dimic
Guest
Posts: n/a
 
      11-23-2007
On Nov 23, 1:25 pm, Joshua Muheim <(E-Mail Removed)> wrote:
> Hi all
>
> a = [ne, :two, :three, :six]
> b = [:two, :four, six]
>
> I want to extract the values of two arrays which exist in both ones (a
> and b):
>
> # => [:two, :six]
>
> Does there exist a method for this?
>
> Thanks
> Josh
> --
> Posted viahttp://www.ruby-forum.com/.


Try
a&b

Dima
 
Reply With Quote
 
 
 
 
Victor Reyes
Guest
Posts: n/a
 
      11-23-2007
Note: parts of this message were removed by the gateway to make it a legal Usenet post.

How about the elements that are in one but not the other array?

Where are these methods documented?

Thank you

Victor

On Nov 23, 2007 7:55 AM, Dejan Dimic <(E-Mail Removed)> wrote:

> On Nov 23, 1:25 pm, Joshua Muheim <(E-Mail Removed)> wrote:
> > Hi all
> >
> > a = [ne, :two, :three, :six]
> > b = [:two, :four, six]
> >
> > I want to extract the values of two arrays which exist in both ones (a
> > and b):
> >
> > # => [:two, :six]
> >
> > Does there exist a method for this?
> >
> > Thanks
> > Josh
> > --
> > Posted viahttp://www.ruby-forum.com/.

>
> Try
> a&b
>
> Dima
>
>


 
Reply With Quote
 
Joshua Muheim
Guest
Posts: n/a
 
      11-23-2007
Thanks! But right - where are they documented?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Garret Kelly
Guest
Posts: n/a
 
      11-23-2007
You're looking for "a - b", in that case.

http://ruby-doc.org/core/classes/Array.html

Hope that helps.

On Nov 23, 2007 8:06 AM, Victor Reyes <(E-Mail Removed)> wrote:
> How about the elements that are in one but not the other array?
>
> Where are these methods documented?
>
> Thank you
>
> Victor
>
>
> On Nov 23, 2007 7:55 AM, Dejan Dimic <(E-Mail Removed)> wrote:
>
> > On Nov 23, 1:25 pm, Joshua Muheim <(E-Mail Removed)> wrote:
> > > Hi all
> > >
> > > a = [ne, :two, :three, :six]
> > > b = [:two, :four, six]
> > >
> > > I want to extract the values of two arrays which exist in both ones (a
> > > and b):
> > >
> > > # => [:two, :six]
> > >
> > > Does there exist a method for this?
> > >
> > > Thanks
> > > Josh
> > > --
> > > Posted viahttp://www.ruby-forum.com/.

> >
> > Try
> > a&b
> >
> > Dima
> >
> >

>


 
Reply With Quote
 
Marcin Raczkowski
Guest
Posts: n/a
 
      11-23-2007
Joshua Muheim wrote:
> Thanks! But right - where are they documented?

in ruby-doc


 
Reply With Quote
 
Garret Kelly
Guest
Posts: n/a
 
      11-23-2007
http://ruby-doc.org/core/classes/Array.html#M002235

--Garret

On Nov 23, 2007 8:21 AM, Joshua Muheim <(E-Mail Removed)> wrote:
> Thanks! But right - where are they documented?
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Victor Reyes
Guest
Posts: n/a
 
      11-23-2007
Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Thank you

On Nov 23, 2007 8:26 AM, Garret Kelly <(E-Mail Removed)> wrote:

> You're looking for "a - b", in that case.
>
> http://ruby-doc.org/core/classes/Array.html
>
> Hope that helps.
>
> On Nov 23, 2007 8:06 AM, Victor Reyes <(E-Mail Removed)> wrote:
> > How about the elements that are in one but not the other array?
> >
> > Where are these methods documented?
> >
> > Thank you
> >
> > Victor
> >
> >
> > On Nov 23, 2007 7:55 AM, Dejan Dimic <(E-Mail Removed)> wrote:
> >
> > > On Nov 23, 1:25 pm, Joshua Muheim <(E-Mail Removed)> wrote:
> > > > Hi all
> > > >
> > > > a = [ne, :two, :three, :six]
> > > > b = [:two, :four, six]
> > > >
> > > > I want to extract the values of two arrays which exist in both ones

> (a
> > > > and b):
> > > >
> > > > # => [:two, :six]
> > > >
> > > > Does there exist a method for this?
> > > >
> > > > Thanks
> > > > Josh
> > > > --
> > > > Posted viahttp://www.ruby-forum.com/.
> > >
> > > Try
> > > a&b
> > >
> > > Dima
> > >
> > >

> >

>
>


 
Reply With Quote
 
Dejan Dimic
Guest
Posts: n/a
 
      11-23-2007
On Nov 23, 3:13 pm, Victor Reyes <(E-Mail Removed)> wrote:
> Note: parts of this message were removed by the gateway to make it a legal Usenet post.
>
> Thank you
>
> On Nov 23, 2007 8:26 AM, Garret Kelly <(E-Mail Removed)> wrote:
>
> > You're looking for "a - b", in that case.

>
> >http://ruby-doc.org/core/classes/Array.html

>
> > Hope that helps.

>
> > On Nov 23, 2007 8:06 AM, Victor Reyes <(E-Mail Removed)> wrote:
> > > How about the elements that are in one but not the other array?

>
> > > Where are these methods documented?

>
> > > Thank you

>
> > > Victor

>
> > > On Nov 23, 2007 7:55 AM, Dejan Dimic <(E-Mail Removed)> wrote:

>
> > > > On Nov 23, 1:25 pm, Joshua Muheim <(E-Mail Removed)> wrote:
> > > > > Hi all

>
> > > > > a = [ne, :two, :three, :six]
> > > > > b = [:two, :four, six]

>
> > > > > I want to extract the values of two arrays which exist in both ones

> > (a
> > > > > and b):

>
> > > > > # => [:two, :six]

>
> > > > > Does there exist a method for this?

>
> > > > > Thanks
> > > > > Josh
> > > > > --
> > > > > Posted viahttp://www.ruby-forum.com/.

>
> > > > Try
> > > > a&b

>
> > > > Dima



Programming Ruby
The Pragmatic Programmer's Guide

class Array
instance methods
& arr & anOtherArray -> anArray

Set Intersection---Returns a new array containing elements common to
the two arrays, with no duplicates.

[ 1, 1, 3, 5 ] & [ 1, 2, 3 ] >> [1, 3]


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Re: Neat way of checking that two hash values both exist? hymie! Perl Misc 1 08-27-2012 07:34 PM
Re: Neat way of checking that two hash values both exist? Tim McDaniel Perl Misc 2 08-27-2012 07:27 PM
Taking action only when two sets of radio buttons have both beenselected Steve Swift Javascript 7 07-20-2008 06:02 AM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM



Advertisments