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; #### $VAR1 = { '6700' => { 'palindromes' => '4', 'chr' => '13', 'exons' => '1', 'dist' => 50 }, '4678' => { 'palindromes' => '0', 'chr' => '6', 'exons' => '2', 'dist' => '45' }, '2346' => { 'palindromes' => '567', 'chr' => '12', 'exons' => '1', 'dist' => '78' }, '5349' => { 'palindromes' => '14', 'chr' => '8', 'exons' => '2', 'dist' => '6' }, '3456' => { 'palindromes' => '5', 'chr' => '10', 'exons' => '1', 'dist' => 67 }, '567' => { 'palindromes' => '8', 'chr' => '5', 'exons' => '7', 'dist' => '344' }, '1345' => { 'palindromes' => '123', 'chr' => '5', 'exons' => '8', 'dist' => '567' }, '8964' => { 'palindromes' => '8', 'chr' => '2', 'exons' => '18', 'dist' => '560' } };