打印间隔而不是时间戳

时间:2012-07-06 14:33:52

标签: perl

以下代码以每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

1 个答案:

答案 0 :(得分: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