in reply to Capturing occurrence counts via tr/// with variable interpolation

$ans = eval "\$str =~ tr/$SrchStr/$SrchStr/"; die $@ if $@;

Update: If you want to count slashes, define $SrchString as '\/', or use a separator other than / for tr. Thanks to Corion for reminding me to point this out.

the lowliest monk

  • Comment on Re: Capturing occurrence counts via tr/// with variable interpolation
  • Download Code

Replies are listed 'Best First'.
Re^2: Capturing occurrence counts via tr/// with variable interpolation
by ysth (Canon) on Aug 15, 2005 at 13:51 UTC
    Better to just use \Q\E:
    $ans = eval "\$str =~ y/\Q$SrchStr\E//";
Re^2: Capturing occurrence counts via tr/// with variable interpolation
by Roger (Parson) on Aug 15, 2005 at 13:56 UTC
    While eval { ... } is Ok, eval " ... " is evil!!!