简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM