Matthew Fischer <[EMAIL PROTECTED]>wrote:
>What is the best way to find the number of occurences of a specific
>character in a string? I have some form data which is in a variable
>$FORM{'countries'}, and I need to know how many pipe characters are
>contained within it.

At 8:56 PM -0700 20/07/01, Dick Applebaum wrote:
>       $x='123|456||78|9|';
>       $z=$x=~s/\|/\|/g;
>       print "$x\n";
>       print "$z\n";


tr/// is slightly more efficient than s///, but it's more awkward in 
the sense it can't do abstract pattern matching so if you don't want 
to mach a character class (\w - a word type character,\d a number 
type character etc), then use tr///.

however the concept is the same:

#!perl
$string="There are a lot of A's in this aaaa";
$test=$string=~tr/a/a/;
print $test;
$test2=$string=~s/\w/\w/g;
print test2;




HTH

Robin

Reply via email to