- 论坛徽章:
- 0
|
本帖最后由 小鹭 于 2010-12-04 23:24 编辑
- #!/usr/bin/perl
- open(FILE,"a.txt") or die("$!");
- @unsorted=<FILE>;
- close(FILE);
- ### 先按照列1,列2排序, 然后得到hash数据
- @sorted = map {$_->[0]}
- sort { $a->[1] <=> $b->[1] or
- $a->[2] <=> $b->[2]
- }
- map {[$_, (split(/\s+/))[0], (split(/\s+/))[1] ]} @unsorted;
- map {($a,$b)=split(/\s+/); $aggr{$a}.=$b . " "} @sorted;
- ### 重点是对组合的结果按照需求进一步处理
- foreach (keys %aggr) {
- @out=split(/\s+/,$aggr{$_});
- $str="";
- for ($i=0; $i<scalar(@out); $i++) {
- if ($out[$i+1] == $out[$i] + 1) {
- $out[$i] .= "-";
-
- } else {
- $out[$i].= ",";
- }
- $str .= $out[$i];
- }
-
- @tmp=split(/,+/,$str);
- map {$_=~s/-.*-/-/g;} @tmp;
- $str=join(",",@tmp);
- print "$_ $str\n";
- }
复制代码 4056 78,198
7717 98,218
7718 98,218
7719 98-99,218-219
7720 98-99,218-220 |
|