#$1 is the matched regex, including duplicated elements. my $match = $1."\n"; my @match_to_array = split("(\n)",$match); my %seen = (); my @r = (); foreach my $a (@match_to_array) { unless ($seen{$a}) { push @r, $a; $seen{$a}++; } } print @r;