Uniq! array of arrays based on one element?

There might be a simple way to do this, but it's been confusing me a
bit.

I have an array of arrays which I made to match the format of an excel
sheet or table.

I need to reject duplicates and only keep one "row" of each based on one
of the
"cells". With a single array, I could use uniq! to do this, but when I'm
comparing arrays against each other using only one of the elements, I'm
not sure how to go about this efficiently.

for example:

array = [[1,a,b],[1,a,c],[2,a,b]]

If I use a given sub array's [0] position as the unique reference, I'd
want to end up with:

array = [[1,a,b],[2,a,b]]

Obviously this is with much larger data but you get the idea.

Thanks for your time.

···

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

uniq has an block form with more magic:

array.uniq! {|x|x[0]}

···

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

Magic indeed! I suspected there would be some Ruby method that would
make an easy job out of a hard one :slight_smile:

Thanks.

···

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

Or a bit prettier:

array.uniq! &:first

···

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

Thanks Jan; although I'm looking at the seventh field in my actual data,
so Hans' approach was more flexible for my purposes :slight_smile:

···

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