in reply to pattern match with different sets.

It would be easier to help you if you posted actual code rather than the vague description "and comparing the $m ==$m1 and also /(\w+)(a|b|c). and comparing the $1 of one sample with the other".

It's only a guess but maybe something like the following would do what you want.

use strict; use warnings; my @samples = qw( PD4005a PD4005b PD4005c WGA_PD4005a WGA_PD4005b WGA_PD4005c DIFFERENT ); foreach my $s1 (@samples) { foreach my $s2 (@samples) { next if(compare($s1,$s2)); print "$s1 - $s2\n"; } } sub compare { my ($s1, $s2) = @_; if($s1 =~ m/^(WGA_)?PD(\d+)[abc]$/) { my $n1 = $2; if($s2 =~ m/^(WGA_)?PD(\d+)[abc]$/) { return($n1 == $2); } } return(0); }