It looks like you're trying to do too much in your code. If you're goal is to just fill in NULL values, then focus on that first.
The below code removes the uniqueness time check that you were doing since it appears superfluous, and relies on the fact that the times are sequential instead of having to do a sort. In the end everything is the same as your code though, the data structures are just simplified and all the validation is done in a single loop
use strict;
use constant NULL_VAL => -999.9;
my @data = ();
while (<DATA>) {
chomp;
s/^\s+|\s+//g;
next if $_ eq '';
push @data, [split ','];
}
my %last_good = map {$_ => NULL_VAL} (1..$#{$data[0]});
# Go in reverse to fill in null data
for (my $i=$#data; $i>=0; $i--) {
# Validate each column
for my $j (1..$#{$data[$i]}) {
if ($data[$i][$j] == NULL_VAL) {
$data[$i][$j] = $last_good{$j};
} else {
$last_good{$j} = $data[$i][$j];
}
}
}
foreach (@data) {
print join(',', @$_) . "\n";
}
# time,col1,col2
# (-999.9 out-of-range to indicate missing
# and distinguish from true zeroes)
__DATA__
0.01,-999.9,1
0.02,-999.9,-999.9
0.03,-999.9,3
0.04,2,-999.9
0.05,-999.9,-999.9
0.06,5,4
Note, this is still potentially fragile in a way though, since it requires loading everything in memory to do the update. But you don't say whether you're doing anything special with this data, like outputting it to a new file.
-
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.