perl 易用的数据结构

1678阅读 0评论2011-06-02 zzy7186
分类:LINUX

根据大哥的博文:http://blog.chinaunix.net/space.php?uid=10540984&do=blog&id=348714
用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.         };
先贴下

上一篇:将单词的首字母转换成大写
下一篇:perl // 操作符 与 ||的区别