Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > matching arrays within arrays

Reply
Thread Tools

matching arrays within arrays

 
 
Nick Black
Guest
Posts: n/a
 
      01-23-2007
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

 
Reply With Quote
 
 
 
 
Alex Young
Guest
Posts: n/a
 
      01-23-2007
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

 
Reply With Quote
 
 
 
 
Chris Gernon
Guest
Posts: n/a
 
      01-23-2007
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/.

 
Reply With Quote
 
Nick Black
Guest
Posts: n/a
 
      01-23-2007
Cheers!

On 1/23/07, Chris Gernon <(E-Mail Removed)> 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

 
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
Understanding Arrays within Arrays MG Javascript 5 04-16-2010 08:08 PM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Using values within arrays within a hash to gsub into an input word... Abe Ruby 2 04-15-2006 08:16 PM
Matching words within two arrays Francesco Moi C++ 1 02-20-2005 10:18 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments