Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370274
  • 博文数量: 61
  • 博客积分: 2451
  • 博客等级: 上尉
  • 技术积分: 650
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-06 21:24
文章分类

全部博文(61)

文章存档

2012年(1)

2011年(44)

2010年(16)

分类: LINUX

2011-06-02 10:38:51

用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:
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. my %stat;
  5. while (<>){
  6.         if($.==1){
  7.                 print;
  8.         }
  9.         else{
  10.                 my($inode,$begin,$end,$count)=split(/\|/,$_);
  11.                 until($stat{$inode}[1] and $stat{$inode}[2])
  12.                 {
  13.                         $stat{$inode}[1]=$begin;
  14.                         $stat{$inode}[2]=$end;
  15.                 }
  16.                 $stat{$inode}[0]+=$count;
  17.                 $stat{$inode}[1]=$stat{$inode}[1]>$begin?$begin:$stat{$inode}[1];
  18.                 $stat{$inode}[2]=$stat{$inode}[2]<$end?$end:$stat{$inode}[2];

  19.         }
  20. }

  21. foreach (sort {$a <=> $b} keys %stat){
  22.         print "$_|$stat{$_}->[1]|$stat{$_}->[2]|$stat{$_}->[0]\n";
  23. }
数据结构:
  1. $VAR1 = {
  2.           '310' => [
  3.                      10103,
  4.                      3337000000,
  5.                      3362120961
  6.                    ],
  7.           '106' => [
  8.                      30000,
  9.                      3363120000,
  10.                      3368579999
  11.                    ],
  12.           '311' => [
  13.                      39900,
  14.                      3313460102,
  15.                      3362120963
  16.                    ]
  17.         };
先贴下

阅读(1641) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~