0

以下代码以每 5 分钟为增量打印最高繁忙值。如何打印增量(例如 02:19:09 的 2:15)而不是时间戳?

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; 

电流输出:

  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
4

1 回答 1

2

你需要提高你在指定问题方面的技能。您已经问了几个不同的问题来达到这一点,主要问题是您对问题的陈述不完整,或者它从一个问题变为下一个问题。

我仍然担心你总是把程序分成两半——输入的处理和输出的生成——我相信这两者可以结合在一起形成一个比你实现的更好、更可靠的解决方案方法。

这是我以前的答案之一的变体,可以满足我的要求。因为您只展示了程序的后半部分,所以我无法提供完整的解决方案。@lastArray此代码希望数据以与您已经使用的代码相同的方式呈现。

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; 

输出

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
于 2012-07-06T17:40:34.170 回答