#!/usr/bin/perl use strict; use warnings; my @data = ("NULL", "NULL", 1, 3, 70, "NULL", "NULL", "NULL", "NULL", "NULL", 50, 1, "NULL", 4, "NULL", "NULL", 5, "NULL", "NULL", "NULL"); my @data_indices = grep { $data[$_] ne "NULL"; } (0 .. $#data); if (@data_indices) { # Phase 1, fill the edges if ($data_indices[0] > 0) { # Left edge map { $data[$_] = $data[$data_indices[0]]; } (0 .. $data_indices[0] - 1); } if ($data_indices[$#data_indices] < $#data) { # Right edge map { $data[$_] = $data[$data_indices[$#data_indices]]; } ($data_indices[$#data_indices] + 1 .. $#data); } # Phase 2, fill all gaps between values for my $index (1 .. $#data_indices) { my $difference = $data_indices[$index] - $data_indices[$index - 1]; if ($difference == 1) { next; } # fill the first half with the leftmost data-element map { $data[$_] = $data[$data_indices[$index - 1]]; } ($data_indices[$index - 1] + 1 .. $data_indices[$index] - $difference / 2); #.. truncates, so /2 will work. # ... and the last half with the rightmost data-element map { $data[$_] = $data[$data_indices[$index]]; } ($data_indices[$index - 1] + 1 + $difference / 2 .. $data_indices[$index] - 1); #.. truncates, so /2 will work. if (($difference % 2) == 0) { # This gap does have an equidistant data-element (which was left-filled, so we'll need to overwrite) my $middle_index = $data_indices[$index] - $difference / 2; $data[$middle_index] = ($data[$middle_index - 1] + $data[$middle_index + 1]) / 2; } } ## end for my $index (1 .. $#data_indices) } ## end if (@data_indices) print (join (',', @data) . "\n");