http://www.perlmonks.org?node_id=911838

dhuang90 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I have been given a large array of numbers, but there are missing values which have a value of NULL. I am to write a program which takes the NULL values and copies the closest value in the array. If a missing value is equidistant between two values, then it will take the average of the two arrays. To illustrate (let underscores represent NULL values: 4 _ _ _ 5 The first NULL would take the value 4, the third NULL takes the value 5, and the middle NULL takes the value 4.5 The code I have written is below, but it doesn't appear to change anything. Any ideas to get me moving? Thanks monks
$left = 1; #measures the distance to the left to the first non-NULL v +alue $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; }