Ruby: Hash {Key => [value1, value2]} - need to check value2 only for each key

Given that i have a dump memory object filled already and i want only to
extract and work with the second value

x = Marshal.load(File.binread(‘in_memory’))

x.each {|key , value | puts " #{key} => #{value}"}

My Result

1 => [[1, 200]]
2 => [[2, 450]]
6 => [[2, 1000]]
7 => [[4, 1003]]
The Marshal binary dump in to an hash has a key and an array of two
values;

in the each loop how can i check for example if value2 is = or > than
1000… if i have some than i only need to display

6 => [[2, 1000]]
7 => [[4, 1003]]
and the count of records found

count =2

i know that value.class = Array
and x.Class = Hash

maybe if transform it into a Map?

y = x.map { |k , v| { k => v} }

ind=0

y.each do |values|

puts ind
puts values
## need to get only the values 1000,1003
ind = ind +1

end

sorry if it is to dummy as question

Franco

h = {1=>[[10,20]],2=>[[30,40]]}
h.each_pair {|k,v| puts “#{k}:#{v[0][1]}”}
=> 1:20 2:40

Mario

Mario C. wrote in post #1169823:

h = {1=>[[10,20]],2=>[[30,40]]}
h.each_pair {|k,v| puts “#{k}:#{v[0][1]}”}
=> 1:20 2:40

Mario

This is Great
Thank you mario

x = Marshal.load(File.binread(‘in_memory’))

x.each_pair {|k,v| puts “#{k} #{v[0][1]}”

if i want to count the entries for specific value for example when v is
3 give true or false if count is greater than 1…

better… how can i trap not the second value but the first value of the
array inside the hash?

if 3 is queried is should get

count = 4

example data
8 [[2, 20150315]]
5 [[3, 20150308]]
1 [[3, 20150308]]
1 [[3, 20150308]]
1 [[3, 20150308]]

Franco C. wrote in post #1169835:

if i want to count the entries for specific value for example when v is
3 give true or false if count is greater than 1…

better… how can i trap not the second value but the first value of the
array inside the hash?

if 3 is queried is should get

h={1 => [[1,100]], 2=> [[2,1000]], 3=> [[3,2000]] }

p h.select {|k,v| v.first.last>=1000}.size
p h.select {|k,v| v.first.first==3}.size

v[0][0] is the 1st and v[0][1] the 2nd element.

Mario

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs