in reply to Simple (probably)

For each hash element, you execute

if ($value =~ /$search/gi) { $contains++; }

$contains++ isn't in any kind of loop, so it gets executed at most once. So let's add the loop we need:

while ($value =~ /$search/gi) { $contains++; }

This is equivalent, yet simpler and faster:

++contains while $value =~ /$search/gi;

And this is faster yet (assuming $search contains no captures):

$contains += () = /$search/gi;

By the way, using m//g in scalar context outside of while condition is a bug.* This includes using it as an if condition.

* — Yes, this is a generalisation, but it's accurate for those don't know what makes the statement incomplete.

Replies are listed 'Best First'.
Re^2: Simple (probably)
by MaroonBalloon (Acolyte) on Dec 13, 2009 at 05:21 UTC
    I appreciate your explanations. Very succinct teacher. Kind of unrelated, but what are captures?
    & What do you mean that m//g is a bug? I am quite a newbie...Thanks!
    -Eric

      what are captures?

      The parens in
      if ('foo 123 bar' =~ /(\d+)/) { print("$1\n"); }
      123

      What do you mean that m//g is a bug?

      for (1..2) { if ('ab' =~ /a/g) { print("match\n"); } else { print("no match\n"); } }
      match no match
      for (1..2) { if ('ab' =~ /a/) { print("match\n"); } else { print("no match\n"); } }
      match match