[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Grouping values of a hash or array.

Adrian Fraiha

8/4/2006 6:03:00 PM

If I've got a hash
>h = { a => 1, b => 2, c => 1, d => 1 }
Is there an easy way to group the values to come out as 1,2?

Same for an away where [1,2,1,2,3] would come out as 1,2,3. I looked
through Ruby API and couldn't find anything. Anyone have any suggestions
as to how to go about this?

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

3 Answers

Berger, Daniel

8/4/2006 6:14:00 PM

0

Adrian Fraiha wrote:
> If I've got a hash
>> h = { a => 1, b => 2, c => 1, d => 1 }
> Is there an easy way to group the values to come out as 1,2?
>
> Same for an away where [1,2,1,2,3] would come out as 1,2,3
^^^^
You're from the Elmer Fudd school of Ruby programming apparently. :-P

> I looked
> through Ruby API and couldn't find anything. Anyone have any suggestions
> as to how to go about this?

See Array#uniq

Regards,

Dan


This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly
prohibited and may be unlawful. If you have received this communication
in error, please immediately notify the sender by reply e-mail and destroy
all copies of the communication and any attachments.

Adrian Fraiha

8/4/2006 6:25:00 PM

0

:D Thanks for the help!

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

Robert Klemme

8/4/2006 7:42:00 PM

0

Adrian Fraiha wrote:
> If I've got a hash
>> h = { a => 1, b => 2, c => 1, d => 1 }
> Is there an easy way to group the values to come out as 1,2?

irb(main):004:0> require 'set'
=> true
irb(main):006:0> { :a => 1, :b => 2, :c => 1, :d => 1 }.values.to_set
=> #<Set: {1, 2}>
irb(main):007:0> { :a => 1, :b => 2, :c => 1, :d => 1 }.values.to_set.to_a
=> [1, 2]

> Same for an away where [1,2,1,2,3] would come out as 1,2,3. I looked
> through Ruby API and couldn't find anything. Anyone have any suggestions
> as to how to go about this?

Same as above - or use uniq as has been mentioned already.

robert