$a = 'PF01389 6 218 1 255 430.09'; $b = 'PF00691 PF01389'; my %counts; foreach ( split /\s+/, $a ) { $counts{$_} = 1; } foreach ( split /\s+/, $b ) { $counts{$_}++ if exists $counts{$_}; } my @common = grep $counts{$_} > 1, keys %counts; if ( @common ) { print "correct\n"; }