This sort of problems can be handled with a DBMS engine.
Something like this?
#!/usr/bin/perl -w
use strict;
use DBI;
my @data = (
['2004-02-01 07:01', 82],
['2004-02-01 11:38', 172],
['2004-02-01 22:48', 154],
['2004-02-02 05:38', 107],
['2004-02-02 13:20', 117],
['2004-02-02 23:48', 188]);
my $dbh = DBI->connect("dbi:SQLite:averages","","")
or die $DBI::errstr;
unless ( -s "averages") {
$dbh->do(qq{ CREATE TABLE averages (
d datetime not null,
val INTEGER)})
or die $DBI::errstr;
for (@data) {
$dbh->do( qq{INSERT INTO averages VALUES (?, ?)}, undef, @$_)
or die $DBI::errstr
}
}
my $query = qq{
SELECT
substr(d,1,10) as day,
CASE
WHEN substr(d,12,2) BETWEEN 4 AND 12 THEN "1 morning"
WHEN substr(d,12,2) BETWEEN 13 AND 18 THEN "2 afternoon"
WHEN substr(d,12,2) BETWEEN 19 AND 24 THEN "3 evening"
ELSE "4 night"
END AS tm,
AVG(val)
FROM
averages
GROUP BY
day, tm
ORDER BY
day, tm
};
my $averages = $dbh->selectall_arrayref($query)
or die $DBI::errstr;
printf "%-10s %-15s %5.2f\n", @$_ for @$averages;
__END__
output:
2004-02-01 1 morning 127.00
2004-02-01 3 evening 154.00
2004-02-02 1 morning 107.00
2004-02-02 2 afternoon 117.00
2004-02-02 3 evening 188.00
You can add/remove/modify the intervals by acting on the CASE statement inside the query.
If you want an average by time only, without the days, then use this query:
my $query = qq{
SELECT
CASE
WHEN substr(d,12,2) BETWEEN 4 AND 12 THEN "1 morning"
WHEN substr(d,12,2) BETWEEN 13 AND 18 THEN "2 afternoon"
WHEN substr(d,12,2) BETWEEN 19 AND 24 THEN "3 evening"
ELSE "4 night"
END AS tm,
AVG(val)
FROM
averages
GROUP BY
tm
ORDER BY
tm
};
my $averages = $dbh->selectall_arrayref($query)
or die $DBI::errstr;
printf "%-15s %5.2f\n", @$_ for @$averages;
__END__
output:
1 morning 120.33
2 afternoon 117.00
3 evening 171.00
-
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.