Reputation: 271
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?
my @maxima;
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] > $value;
}
}
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
Mon,Jun,25,03:40:11,2012,999,1,0,1,0,0,0,0,1,0,0
Upvotes: 0
Views: 193
Reputation: 126722
You need to work on your skills at specifying problems. You have asked several different questions to get to this point, and the main problem is that your statement of the problem is incomplete, or it changes from one question to the next.
I am still worried that you always show the program in two halves - the processing of the input and the generation of the output - and I am sure that the two can be combined together to form a better and more reliable solution than you have achieved this way.
Here is a variation of one of my previous answers that does what I think you want. Because you have shown only the second half of your program I can't provide a complete solution. This code expects data to be presented in @lastArray
in the same way as the code you have used already.
my @maxima;
for my $record (@lastArray) {
my @fields = split /,/, $record;
next unless grep $_, @fields;
$fields[3] =~ s|(\d+):\d\d$|sprintf '%02d', 5*int($1/5)|e;
$record = join ',', @fields;
my $key = join ' ', @fields[1..4];
my $value = $fields[5];
if (@maxima == 0 or $key ne $maxima[-1][0]) {
push @maxima, [$key, $value, $record];
}
else {
@{$maxima[-1]}[1,2] = ($value, $record) if $maxima[-1][1] > $value;
}
}
print $_->[2] for @maxima;
output
Mon,Jun,25,02:15,2012,999,1,0,1,0,0,0,0,1,0,0
Mon,Jun,25,02:20,2012,999,1,0,1,0,0,0,0,1,0,0
Mon,Jun,25,02:25,2012,999,1,0,1,0,0,0,0,1,0,0
Mon,Jun,25,02:55,2012,999,1,0,1,0,0,0,0,1,0,0
Mon,Jun,25,03:00,2012,999,1,1,0,0,0,0,0,0,0,0
Mon,Jun,25,03:05,2012,999,1,0,1,0,0,0,0,1,0,0
Mon,Jun,25,03:10,2012,999,1,0,1,0,0,0,0,1,0,0
Mon,Jun,25,03:20,2012,999,1,0,1,0,0,0,0,1,0,0
Mon,Jun,25,03:35,2012,999,1,0,0,0,0,0,1,0,0,0
Mon,Jun,25,03:40,2012,999,1,0,1,0,0,0,0,1,0,0
Upvotes: 2