Dr Eberhard W Lisse <nos...@lisse.NA> writes:
[...]
> [...]
> 2022-06-19: 0.855
> 2022-06-21: 0.8601
> 2022-06-22: 0.8589
> 2022-06-23: 0.8582
> 2022-06-29: 0.8646
> [...]
>
> which is non-continuous mainly because of weekends and so on.
>
>
> How do I fill up the empty key-value pairs by way of the last existing
> pair?
Not overly complicated way of doing that:
------
#* date generator
#
my @m_lens = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
sub m_len
{
my ($y, $m) = @_;
my $ml;
$ml = $m_lens[$m - 1];
++$ml if $ml < 30 && !($y & 3) && !($y % 100 == 0 && $y % 400);
return $ml;
}
sub date_counter
{
my ($y, $m, $d) = split('-', $_[0]);
return sub {
++$d;
if ($d > m_len($y, $m)) {
$d = 1;
if ($m < 12) {
++$m;
} else {
$m = 1;
++$y;
}
}
return sprintf('%u-%02u-%02u', $y, $m, $d);
}
}
#* sample input data
#
my %data = qw(2022-06-19 0.855
2022-06-21 0.8601
2022-06-22 0.8589
2022-06-23 0.8582
2022-06-29 0.8646);
#* main
#
my @dates = sort(keys(%data));
my $dc = date_counter($dates[0]);
my ($cur_v, $n_d);
$cur_v = $data{$dates[0]};
for (@dates[1 .. $#dates]) {
$data{$n_d} = $cur_v while ($n_d = $dc->()) ne $_;
$cur_v = $data{$_};
}
print($_, "\t", $data{$_}, "\n") for sort(keys(%data));