matching arrays within arrays

Discussion in 'Ruby' started by Nick Black, Jan 23, 2007.

  1. Nick Black

    Nick Black Guest

    Hello,

    I have a number of arrays within arrays. Some of them are duplicatess like:

    [[foo, bar, ru, by], [ru], [bar, foo], [foo, bar, ru, by]]

    I would like to remove duplicates to end up with:

    [[foo, bar, ru, by], [ru], [bar, foo]]

    Is there a Ruby function to do this?


    Cheers,


    --
    Nick Black
    --------------------------------
    http://www.blacksworld.net
     
    Nick Black, Jan 23, 2007
    #1
    1. Advertising

  2. Nick Black

    Alex Young Guest

    Nick Black wrote:
    > Hello,
    >
    > I have a number of arrays within arrays. Some of them are duplicatess
    > like:
    >
    > [[foo, bar, ru, by], [ru], [bar, foo], [foo, bar, ru, by]]
    >
    > I would like to remove duplicates to end up with:
    >
    > [[foo, bar, ru, by], [ru], [bar, foo]]
    >
    > Is there a Ruby function to do this?


    irb(main):007:0> [['foo', 'bar', 'ru', 'by'], ['ru'], ['bar', 'foo'],
    ['foo', 'bar', 'ru', 'by']].uniq
    => [["foo", "bar", "ru", "by"], ["ru"], ["bar", "foo"]]

    --
    Alex
     
    Alex Young, Jan 23, 2007
    #2
    1. Advertising

  3. Nick Black

    Chris Gernon Guest

    Nick Black wrote:
    > Is there a Ruby function to do this?


    uniq will work. It doesn't matter what type of object the members of the
    array are, so it can be used for an array of arrays.

    irb> [['foo', 'bar', 'ru', 'by'], ['ru'], ['bar', 'foo'], ['foo', 'bar',
    'ru', 'by']].uniq
    => [["foo", "bar", "ru", "by"], ["ru"], ["bar", "foo"]]

    --
    Posted via http://www.ruby-forum.com/.
     
    Chris Gernon, Jan 23, 2007
    #3
  4. Nick Black

    Nick Black Guest

    Cheers!

    On 1/23/07, Chris Gernon <> wrote:
    > Nick Black wrote:
    > > Is there a Ruby function to do this?

    >
    > uniq will work. It doesn't matter what type of object the members of the
    > array are, so it can be used for an array of arrays.
    >
    > irb> [['foo', 'bar', 'ru', 'by'], ['ru'], ['bar', 'foo'], ['foo', 'bar',
    > 'ru', 'by']].uniq
    > => [["foo", "bar", "ru", "by"], ["ru"], ["bar", "foo"]]
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Nick Black
    --------------------------------
    http://www.blacksworld.net
     
    Nick Black, Jan 23, 2007
    #4
    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. Francesco Moi

    Matching words within two arrays

    Francesco Moi, Feb 20, 2005, in forum: C++
    Replies:
    1
    Views:
    331
    Thomas Matthews
    Feb 20, 2005
  2. Abe
    Replies:
    2
    Views:
    171
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    238
    Marc Bissonnette
    Jan 13, 2004
  4. Bobby Chamness
    Replies:
    2
    Views:
    235
    Xicheng Jia
    May 3, 2007
  5. MG
    Replies:
    5
    Views:
    189
    Thomas 'PointedEars' Lahn
    Apr 16, 2010
Loading...

Share This Page