Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

hash - Compare keys of array in RUBY

I have this structure:

$ArrayX = [8349310431,8349314513,......]
$ArrayY = [667984788,667987788,......]
$ArrayZ = [148507632380,153294624079,.....]

$range_map = $ArrayX.zip([$ArrayY.map(&:to_i), 
             $ArrayZ.map(&:to_i)].transpose).sort

puts $range_map ={[8349310431=>[667984788, 148507632380],  
                 8349314513=>[667987788, 153294624079]}

I need the key to be compared with the rest of the keys and if the subtraction between keys is lower than 100, that key to print

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I corrected your code also as per your need, and solved further,

$ArrayX = [8349310431,8349314513]
$ArrayY = [667984788,667987788]
$ArrayZ = [148507632380,153294624079]

$range_map = $ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort

$ArrayX = [8349310431,8349314513]
 => [8349310431, 8349314513]
$ArrayY = [667984788,667987788]
 => [667984788, 667987788]
$ArrayZ = [148507632380,153294624079]
 => [148507632380, 153294624079] 

$range_map = Hash[$ArrayX.zip([$ArrayY.map(&:to_i), $ArrayZ.map(&:to_i)].transpose).sort]
 => {8349310431=>[667984788, 148507632380], 8349314513=>[667987788, 153294624079]}

keys = $range_map.keys
valid_keys = keys.select { |k| keys.detect { |x| (x-k).abs > 100 } }
$range_map.slice(*valid_keys)

If particular key is having difference more than 100 with one of rest of keys then it will be valid for filtering.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...