in reply to Fetching 3 letters only

$_ = q(fileCapaber); my $count = ($_ =~ y/abcABC/abcABC/); print $count;
artist

Replies are listed 'Best First'.
Re: Re: Fetching 3 letters only
by Anonymous Monk on May 23, 2003 at 19:25 UTC
    Thanks. Actually I have several words to fetch so I need a reg expression to fetch the specific letters "a" "b" "c"

      If you only want the total

      my $s = 'fileCapaber'; my $count = () = $s =~ m[(a|b|c)]gi; print $count; 4

      but the tr/// solution above is easier and quicker.

      If you want the individual counts, then you could try this.

      print 'Found: ', scalar( () = $s =~ m/($_)/ig), "'${_}'s\n" for qw[a b + c] Found: 2 'a's Found: 1 'b's Found: 1 'c's

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller