foreach my $f (@l1){ if (join(",",@l2) !~ /\b$f\b/){ $f='null'; } }