#!/usr/bin/perl -w use strict; use warnings; my %hash; while () { my ($coord, $dist) = split; $hash{$coord} = $dist unless defined $hash{$coord} && $dist > $hash{$coord}; } print "$_\t$hash{$_}\n" foreach (sort { $a <=> $b } keys %hash); __DATA__ 567 344 1345 567 2346 78 3456 67 3456 789 4678 45 5349 6 6700 124 6700 50 8964 560