in reply to computation difficulty

I'd...

which leads to something like...

use constant START_MINS => 180; use constant PERIOD => 20; my @arrivals = qw/03:05 03:17 03:23/; @arrivals = map { as_minutes( $_ ) } @arrivals; for my $time ( START_MINS+1 .. START_MINS+8 ) { my @arrivals_this_period = grep { $_ > $time && $_ <= $time + PERIOD } @arrivals; print as_hhmm( $time ) , ": ", scalar @arrivals_this_period, "\n"; } sub as_minutes { my ( $hh, $mm ) = split /:/, shift; return $hh*60 + $mm; } sub as_hhmm { my $minutes = shift; my $hh = int $minutes / 60; my $mm = $minutes - $hh * 60; return sprintf "%02d:%02d", $hh, $mm; }

Output:

03:01: 2 03:02: 2 03:03: 3 03:04: 3 03:05: 2 03:06: 2 03:07: 2 03:08: 2

There's still the problem of your 20 minute period crossing midnight though

update: Fixed the for my $time ... line to match the output I gave

Replies are listed 'Best First'.
Re^2: computation difficulty
by ack (Deacon) on Apr 04, 2008 at 17:50 UTC

    For your solution it would probably be better to work in 'epoch minutes' (i.e., minutes some some epoch...i.e., reference...date) rather than 'daily minutes' (i.e., minutes since midnight).

    There are time-handling modules in CPAN that can handle it and there are plenty of formulas out on the internet that are pretty simple to compute Julian Dates (i.e., time since some 'standard' epoch, where Julian Date is usually expressed in elapsed decimal days since a 'standard' date...e.g., days since noon on 1 Jan 2000). This avoids the ...crossing of midnight... issue that you noted can be tricky.

    ack Albuquerque, NM