- 論壇徽章:
- 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";
- }
復制代碼 |
|