Newsgroups: comp.lang.perl Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!convex!usenet From: Tom Christiansen Subject: Re: tr doesn't count Message-ID: <1991Jun25.181707.7846@convex.com> Sender: usenet@convex.com (news access account) Nntp-Posting-Host: pixel.convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX References: Date: Tue, 25 Jun 1991 18:17:07 GMT Lines: 47 From the keyboard of cluther@sonne.cnns.unt.edu (Clay Luther): :Given, : :$c = "3/3/3"; :$c = tr/\//\//; : :Why is $c set to 0 and not 2? Because $_ (the default operand for the tr/// operator) doesn't have any slashes in it. If you had done this: $_ = "3/3/3"; $c = tr/\//\//; It would have done what you wer expecting. Try this: $string = "3/3/3"; then $count = ( $string =~ tr#/#/#; ); which is the same as: $count = $string =~ tr#/#/#;; but since I'm never 100.000000000% sure it's not ($count = $string) =~ tr#/#/#; I use the parens anyway. With recent patches, you could also use this: $count++ while $string =~ m#/#g; Although the tr/// runs a good deal faster. 40% the time for m//g in this case, even better with more things to match. I guess the moral is for counting single chars, use tr///, but for counting longer patterns, you'll need m//g. --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."