- 论坛徽章:
- 307
|
本帖最后由 sunzhiguolu 于 2015-10-02 15:45 编辑
回复 9# 赛萌
抱歉, 在看问题的时候理解的有问题. 我将代码修改了一下, 再试试...- #!/usr/bin/perl
- use 5.010;
- use strict;
- use warnings;
- sub set_or_get_last_id{
- state $last_id;
- $last_id = shift if (@_);
- $last_id;
- }
- my %keys;
- while (<>){
- chomp;
- my @a_line = split /\s+/;
- my (undef, $id) = (@a_line);
- if (m/^>/ and !(exists $keys{$id})){
- set_or_get_last_id($id);
- $keys{$id} = undef;
- }else{
- my $current_str = $a_line[0];
- my $last_id = set_or_get_last_id();
- $keys{$last_id} = $current_str if (length($current_str) > length($keys{$last_id}));
- }
- }
- while (my ($id, $value) = each %keys){
- say "> $id\n$value";
- }
复制代码 |
|