compare two decimal values with specific range in perl -


i new perl. have 2 different sets of arrays. 2 arrays having decimal values, both not have exact matches, might differ either +0.5 or -0.5. tried program, it's not working.

@inpt = qw(1003.3965 1036.4392 1037.3843 1045.4459 1101.4259 1107.4253 1118.3928 1191.4904 1320.4855 1420.6291 1440.6921 1562.6698 1742.7587 2084.9137 2248.8761 2337.8865 2628.9931); @outpt = qw(1191.6017 1101.5336 2629.2865 1742.9336 1036.5726 2338.1574 2249.1057 1440.8222 1440.2074);  foreach $s (@outpt){      $inc = $s + 0.5;     $dec = $s - 0.5;     foreach $p ( @inpt ) {         if ( $p .. $inc ) {                        print "$p \t $inc";         }         elsif ( $p .. $dec ) {             print "$p \t $dec";         }     } } 

i expected below output. @outpt value of 1191.6017 matching range of +0.5 or -0.5 in @inpt value of 1191.4904.

outpt          inpt 1191.6017     1191.4904 1101.5336     1101.4259 2629.2865     2628.9931 1742.9336     1742.7587 1036.5726     1037.3843 2338.1574     2337.8865 2249.1057     2248.8761 1440.8222     1440.6291 1440.2074     1440.6291 

try this:

use strict; use warnings;  $limit = 0.5; $result = "";  @inpt = qw(1003.3965 1036.4392 1037.3843 1045.4459 1101.4259 1107.4253 1118.3928 1191.4904 1320.4855 1420.6291 1440.6921 1562.6698 1742.7587 2084.9137 2248.8761 2337.8865 2628.9931); @outpt = qw(1191.6017 1101.5336 2629.2865 1742.9336 1036.5726 2338.1574 2249.1057 1440.8222 1440.2074);  foreach $s (@outpt){    foreach $t (@inpt) {       if($s >= $t - $limit && $s <= $t + $limit) {           $result .= "$s \t $t\n";       }    } }      print $result; 

Comments