... my $past=""; for my $record (sort @records){ chomp $record; if ($record !~ /^$past/{ push @uniq, $past; } $past=$record; } push @uniq, $past; ...