用perl 实现了下 感觉code很长啊
文本:
inode|beginnumber|endnumber|counts|
106|3363120000|3363129999|10000|
106|3368560000|3368579999|20000|
310|3337000000|3337000100|101|
310|3342950000|3342959999|10000|
310|3362120960|3362120961|2|
311|3313460102|3313469999|9898|
311|3313470000|3313499999|30000|
311|3362120962|3362120963|2|
要求将以上文本以inode为标记,对counts进行累加,并且统计出beginnumber的最小值和endnumber的最大值:
inode|beginnumber|endnumber|counts|
106|3363120000|3368579999|30000|
310|3337000000|3362120961|10103|
311|3313460102|3362120963|39900|
下面是code:
- #!/usr/bin/perl
-
use warnings;
-
use strict;
-
my %stat;
-
while (<>){
-
if($.==1){
-
print;
-
}
-
else{
-
my($inode,$begin,$end,$count)=split(/\|/,$_);
-
until($stat{$inode}[1] and $stat{$inode}[2])
-
{
-
$stat{$inode}[1]=$begin;
-
$stat{$inode}[2]=$end;
-
}
-
$stat{$inode}[0]+=$count;
-
$stat{$inode}[1]=$stat{$inode}[1]>$begin?$begin:$stat{$inode}[1];
-
$stat{$inode}[2]=$stat{$inode}[2]<$end?$end:$stat{$inode}[2];
-
-
}
-
}
-
-
foreach (sort {$a <=> $b} keys %stat){
-
print "$_|$stat{$_}->[1]|$stat{$_}->[2]|$stat{$_}->[0]\n";
-
}
数据结构:
- $VAR1 = {
-
'310' => [
-
10103,
-
3337000000,
-
3362120961
-
],
-
'106' => [
-
30000,
-
3363120000,
-
3368579999
-
],
-
'311' => [
-
39900,
-
3313460102,
-
3362120963
-
]
-
};
先贴下
阅读(1677) | 评论(0) | 转发(0) |