use warnings; use strict; open my $data, '<', 'alldata.txt' or die "Could not open 'alldata.txt': $!\n"; my %dataset; RECORD: while ( my $line = <$data> ) { chomp $line; next RECORD unless $line =~ m{\d}; my ( $coord, $dist, $chr, $exons, $pals ) = split /\s+/, $line; next RECORD if exists $dataset{$coord} and $dataset{$coord}{dist} < $dist; $dataset{$coord} = { dist => $dist, chr => $chr, exons => $exons, palindromes => $pals, }; } say "coord\tdist\tchr\texons\tpalindromes"; say "$_\t$dataset{$_}{dist}\t$dataset{$_}{chr}\t$dataset{$_}{exons}\t" . "$dataset{$_}{palindromes} " for sort { $a <=> $b } keys %dataset;