Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find in a hash any values greater then a specific value

Tags:

ruby

Suppose I have this hash

{ 1 => 4 , 2 => 3 , 4 => 1}

Now I want to make a loop where I can find any value which is greater then 3.

I know with hash.values?3 I can find the pairs has a value of 3. But how can I find all the values which are greater of equal as 3.

Roelof

Edit 1 : I try to make this pseudo code at work in ruby.

while there are sets of 3 ones:
  remove the set from the hash (h[1] -= 3)
  sum 1000 to the score
end
like image 999
Kern Twente Avatar asked Dec 12 '22 22:12

Kern Twente


1 Answers

Use #select method:

{a: 1, b: 2, c: 3}.select{|k,v| v > 1}
like image 101
Hck Avatar answered Dec 14 '22 12:12

Hck