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;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.