use warnings; use strict; my @array = <>; my @uniq= map { $_->[0] } grep { ! $_->[1] } sort { my $v = $a->[0] cmp $b->[0]; $a->[1] = 1 unless $v or $b->[1]; $v } map { [$_] } @array; print @uniq;