Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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; }

In reply to Filling in missing values in an array by dhuang90

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-20 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found