$ perl -Mstrict -Mwarnings -E ' my @ary = qw{xyz xyx abc xxx def zyx}; my $sub = q{x}; my @counts = map { scalar @{[/$sub/g]} } grep { /$sub/ } @ary; say "All strings = ", scalar @ary; say "Strings with $sub in them = ", scalar @counts; say "Counts of $sub in strings containing $sub:"; say for @counts; ' All strings = 6 Strings with x in them = 4 Counts of x in strings containing x: 1 2 3 1