my (%first, %last); while () { next unless /^(Query|Sbjct):\s+(\d+)\s+(?:.*)\s(\d+)\s*$/; $last{$1}= $3; next if exists $first{$1}; $first{$1}=$2; } print<