- 论坛徽章:
- 307
|
本帖最后由 sunzhiguolu 于 2015-09-28 17:32 编辑
回复 1# 赛萌
您的代码好多地方, 我还没有接触到. 所以不能为您指出问题, 抱歉.
在没有更好的解决方案前, 可以试下我这个, 等有了更好的方法再更换好了! (笨方法)- #!/usr/bin/perl
- use 5.010;
- use strict;
- use warnings;
- #函数 set_or_get_last_id 用来存放或获取上一次的 $id 的值
- sub set_or_get_last_id{
- state $last_id;
- $last_id = shift if (@_);
- $last_id;
- }
- #哈希变量 %keys_name, %keys_value 并行存储 ($id, $name) 及 ($id, $max_long_str)
- my (%keys_name, %keys_value, $max_long_str);
- while(<>){
- my ($name, $id);
- chomp;
- my @a_line = split /\s+/;
- if (m/^>/){
- ($name, $id) = ($a_line[0], $a_line[1]);
- if (!(exists $keys_name{$id})){
- $name =~ s/\.[0-9]+$//; #加入这句
- $keys_name{$id} = $name;
- $keys_value{$id} = "";
- set_or_get_last_id($id);
- }
- }else{
- my $last_id = set_or_get_last_id();
- $max_long_str = $keys_value{$last_id};
- my $current_str = $a_line[0];
- if (length($current_str) > length($max_long_str)){
- $keys_value{$last_id} = $current_str;
- }
- }
- }
- foreach (keys %keys_name){
- print "$keys_name{$_}\n$keys_value{$_}\n";
- }
复制代码 |
|