foreach $key ( sort (keys(%exchangrate))) {
print "$key: $exchangrate{$key}\n"
}
[...]
results in something like
[...]
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?
Ie in this example, the value for 2022-06-20 should also be 0.855, and
the values for 2022-06-24/5/6/7/8 should all be 0.8582.
El 3/7/22 a las 17:14, Dr Eberhard W Lisse escribió:
foreach $key ( sort (keys(%exchangrate))) {
print "$key: $exchangrate{$key}\n"
}
[...]
results in something like
[...]
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?
Ie in this example, the value for 2022-06-20 should also be 0.855, and
the values for 2022-06-24/5/6/7/8 should all be 0.8582.
for $key ( sort keys %exchangerate ) {
if ($key =~ /(\d+)\-(\d+)\-(\d+)/) {
my $date = $3;
if ($date <= 30){
my $c = 1
$c++;
}
}
}
}
Try this. Untested.
for $key ( sort keys %exchangerate ) {
if ($key =~ /(\d+)\-(\d+)\-(\d+)/) {
my $date = $3;
if ($date <= 30){
my $c = 1
$c = spritf "%02d", $date+$c; # let's try to fix things
until (exists $exchagerate{"$1-$2-$c"}){
$exchangerate{"$1-$2-$c"} =$exchangerate{$key};
$c++;
$c = sprintf "%02d", $c; #
}
}
}
}
Try this. Untested.
#!/usr/bin/perl[...]
A question arises here. For how many days have sense to have[...]
no data? Is it possible to happen in the edge of one month
to another? Then, similar arrangements should be done to the
number of month, and so on.
[...]
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?
# This is just to get some values into %Rate for the demo:
# You should populate %Rate properly with your database
# selection code (as %exchangrate in your post).
my %Rate = (
'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,
);
my @Dates = sort keys %Rate;
sub previous_date {
my $date = $_[0];
return $date if exists $Rate{$date};
return (grep $_ lt $date, @Dates)[-1];
}
# Iterate through every date between the first value and
# the last value in @Dates:
for (
my $date = $Dates[0];
$date le $Dates[-1];
$date = iso8601( timestamp($date)+86400 )
) {
next if exists $Rate{$date};
my $previous = previous_date($date);
$Rate{$date} = $Rate{$previous};
}
Bob Nichols <[email protected]d> writes:[...]
With doing a benchmark, the exact effects of this can't be determined
(and they might not matter much, anyway) but this is a bad algorithm:
It iterates over an array of a certain length and scans the complete
array once for each iteration, hence, it's running time is
proportional to the square of the array length (ie, the algorithm is
of quadratic complexity).
Not overly complicated way of doing that:[...]
Currently I look up the exchange rate for each transaction (between 2
and 100, last week, last month up to the whole of a calendar year) with
an SQL statement which gives me the last one before the date if the date doesn't have one.
This is reasonably fast, but sending a single SQL statement from Perl
and then doing that with a hash would be obviously faster and some fun figuring out
I have modified both to produce identical output (4489 "completed' pairs
from 3897 returned by my SQL statement and then a few identical lines
to measure the time the actual code (without the SQL) takes
Your's takes indeed around 6 milliseconds whereas Bob's takes some 136 milliseconds (on my iMac) without the (print(f) statement(s)).
Dr Eberhard Lisse <[email protected]> writes:
I have modified both to produce identical output (4489 "completed'
pairs from 3897 returned by my SQL statement and then a few identical
lines to measure the time the actual code (without the SQL) takes
Your's takes indeed around 6 milliseconds whereas Bob's takes some
136 milliseconds (on my iMac) without the (print(f) statement(s)).
That's interesting. I actually expected the opposite result. While I
was using a linear algorithm, the implementation (with a date
enumeration closure) wasn't exactly low-overhead and did a lot of
stuff in Perl. This usually means that the theoretical scalability
advantage doesn't turn into a practical advantage unless the amount of
input data is insanely large.
In article <[email protected]>, Dr Eberhard Lisse <[email protected]> wrote:
Currently I look up the exchange rate for each transaction (between 2
and 100, last week, last month up to the whole of a calendar year)
with an SQL statement which gives me the last one before the date if
the date doesn't have one.
This is reasonably fast, but sending a single SQL statement from Perl
and then doing that with a hash would be obviously faster and some
fun figuring out
You could use a temporary SQLite database to cache an appropriate
range of data from the remote server. Looking up individual dates in
this cache would be very fast, possibly faster than any
fill-in-the-holes code written in Perl.
/Bo Lindbergh
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 26:17:34 |
| Calls: | 12,106 |
| Calls today: | 6 |
| Files: | 15,006 |
| Messages: | 6,518,193 |