$left = 1; #measures the distance to the left to the first non-NULL value $right = 1; #measures the distance to the right to the first non-NULL value $q = 0; #used to iterate through the exposures for the first time $p = 0; #used to find a non-NULL value to the left, first iteration $k = 0; #used to find a non-NULL value to the right, first iteration $m = 0; #used to iterate through the exposures for the second time $n = 0; #used to find a non-NULL value to the left, second iteration $l = 0; #used to find a non-NULL value to the right, second iteration $o = 0; #used to assign values using an average, third iteration foreach $exp (@exposures) { if ($exp eq "NULL") { while ($exposures[$q - $p] eq "NULL") { ++$left; ++$p; } while($exposures[$q + $k] eq "NULL") { ++$right; ++$k; } } if ($left < $right) { $data{$q} = "L"; } if ($left > $right) { $data{$q} = "R"; } $left = 1; $right = 1; $p = 0; $k = 0; ++$q; } $end = $#exposures; $number_of_exposures = $end + 1; while ($m <= $number_of_exposures) { if ($data{$m} eq "L") { while ($exposures[$m - $n] eq "NULL") { ++$n; } $exposures[$m] = $exposures[4]; } if ($data{$m} eq "R") { while ($exposures[$m + $l] eq "NULL"){ ++$l; } $exposures[$m] = $exposures[4]; } $n = 0; $l = 0; ++$m; } foreach $exp (@exposures) { if ($exp eq "NULL") { $exp = ($exposures[$o - 1] + $exposures[$o + 1])/2; } ++$o; }