Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The following code prints the highest busy value in every 5 minute increment. How can I print the increment (ex. 2:15 for 02:19:09) instead of the timestamp?

for my $record (@lastArray) { my @fields = $record =~ /([^,\s]+)/g; next unless @fields; my @range = @fields[1..4]; $range[2] =~ s|(\d+):\d\d$|5*int($1/5)|e; my $range = join ' ', @range; my $value = $fields[5]; if (@maxima == 0 or $range ne $maxima[-1][0]) { push @maxima, [$range, $value, $record]; } else { @{$maxima[-1]}[1,2] = ($value, $record) if $maxima[-1][1] > $v +alue; } } print $_->[2] for @maxima;

Current output

Mon,Jun,25,02:19:09,2012,999,1,0,1,0,0,0,0,1,0,0 Mon,Jun,25,02:21:09,2012,999,1,0,1,0,0,0,0,1,0,0 Mon,Jun,25,02:25:10,2012,999,1,0,1,0,0,0,0,1,0,0 Mon,Jun,25,02:56:10,2012,999,1,0,1,0,0,0,0,1,0,0 Mon,Jun,25,03:00:10,2012,999,1,1,0,0,0,0,0,0,0,0 Mon,Jun,25,03:08:10,2012,999,1,0,1,0,0,0,0,1,0,0 Mon,Jun,25,03:10:10,2012,999,1,0,1,0,0,0,0,1,0,0 Mon,Jun,25,03:24:11,2012,999,1,0,1,0,0,0,0,1,0,0 Mon,Jun,25,03:37:11,2012,999,1,0,0,0,0,0,1,0,0,0

Replies are listed 'Best First'.
Re: Print Interval Instead of Timestamp
by zentara (Cardinal) on Jul 06, 2012 at 16:01 UTC
    Do you mean you want your timestamps to go from something like 0:05 ... 0:55..1:00 ...1:10...2:05... etc etc? How would you know the exact time it actually ran if you impose an artificial timestamp on it?

    Would a loop work?

    #!/usr/bin/perl use strict; use warnings; my $hour = 0; my @mins = ('00','05','10','15','20','25','30','35','40','45','50','55 +'); for my $hour (0..23){ for (1..12 ){ print $hour.':'.$mins[0],"\n"; push (@mins,shift(@mins)); #go round the clock } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Another approach would be to make a simple subroutine that rounds the time down to the nearest 5-minute mark. This would be a lot easier to maintain (and change the interval, if desired). It would basically go something like this:

      1. adjust minute value by adding seconds / 60
      2. use Math::Round to round down to the nearest multiple of choice (use a variable instead of 5 to make it easily customizable):
      3. $minutes_binned = nearest_floor( 5, $minutes_floating );
      4. return binned minute
Re: Print Interval Instead of Timestamp
by frozenwithjoy (Priest) on Jul 06, 2012 at 16:14 UTC
    As I understand it, you essentially want your times to be binned, like in a histogram, right? If the output you are showing is your final output, my suggestion may be overkill, but if it is an intermediate form of the data and you plan to go further, you should check out Math::SimpleHisto::XS or Math::Histogram.
Re: Print Interval Instead of Timestamp
by Anonymous Monk on Jul 06, 2012 at 15:56 UTC

    How can I print the increment (ex. 2:15 for 02:19:09) instead of the timestamp?

    Increment?