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

Discussion in 'Ruby' started by Joshua Muheim, Nov 23, 2007.

  1. Hi all

    a = [:eek:ne, :two, :three, :six]
    b = [:two, :four, :five: 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/.
     
    Joshua Muheim, Nov 23, 2007
    #1
    1. Advertising

  2. Joshua Muheim

    Dejan Dimic Guest

    On Nov 23, 1:25 pm, Joshua Muheim <> wrote:
    > Hi all
    >
    > a = [:eek:ne, :two, :three, :six]
    > b = [:two, :four, :five: 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 :)
     
    Dejan Dimic, Nov 23, 2007
    #2
    1. Advertising

  3. Joshua Muheim

    Victor Reyes Guest

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

    > On Nov 23, 1:25 pm, Joshua Muheim <> wrote:
    > > Hi all
    > >
    > > a = [:eek:ne, :two, :three, :six]
    > > b = [:two, :four, :five: 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 :)
    >
    >
     
    Victor Reyes, Nov 23, 2007
    #3
  4. Re: Two arrays: return only values that exist in both arrays

    Thanks! But right - where are they documented?
    --
    Posted via http://www.ruby-forum.com/.
     
    Joshua Muheim, Nov 23, 2007
    #4
  5. Joshua Muheim

    Garret Kelly Guest

    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 <> 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 <> wrote:
    >
    > > On Nov 23, 1:25 pm, Joshua Muheim <> wrote:
    > > > Hi all
    > > >
    > > > a = [:eek:ne, :two, :three, :six]
    > > > b = [:two, :four, :five: 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 :)
    > >
    > >

    >
     
    Garret Kelly, Nov 23, 2007
    #5
  6. Re: Two arrays: return only values that exist in both arrays

    Joshua Muheim wrote:
    > Thanks! But right - where are they documented?

    in ruby-doc :)
     
    Marcin Raczkowski, Nov 23, 2007
    #6
  7. Joshua Muheim

    Garret Kelly Guest

    Garret Kelly, Nov 23, 2007
    #7
  8. Joshua Muheim

    Victor Reyes Guest

    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 <> 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 <> 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 <> wrote:
    > >
    > > > On Nov 23, 1:25 pm, Joshua Muheim <> wrote:
    > > > > Hi all
    > > > >
    > > > > a = [:eek:ne, :two, :three, :six]
    > > > > b = [:two, :four, :five: 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 :)
    > > >
    > > >

    > >

    >
    >
     
    Victor Reyes, Nov 23, 2007
    #8
  9. Joshua Muheim

    Dejan Dimic Guest

    On Nov 23, 3:13 pm, Victor Reyes <> 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 <> 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 <> 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 <> wrote:

    >
    > > > > On Nov 23, 1:25 pm, Joshua Muheim <> wrote:
    > > > > > Hi all

    >
    > > > > > a = [:eek:ne, :two, :three, :six]
    > > > > > b = [:two, :four, :five: 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]
     
    Dejan Dimic, Nov 23, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ABCL
    Replies:
    0
    Views:
    566
  2. Kev Jackson
    Replies:
    2
    Views:
    131
  3. Steve Swift
    Replies:
    7
    Views:
    117
    Steve Swift
    Jul 20, 2008
  4. Tim McDaniel
    Replies:
    2
    Views:
    385
    Tim McDaniel
    Aug 27, 2012
  5. hymie!
    Replies:
    1
    Views:
    368
    Tim McDaniel
    Aug 27, 2012
Loading...

Share This Page