use strict; use constant NULL_VAL => -999.9; my @data = (); while () { 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