cluther@sonne.cnns.unt.edu (Clay Luther) (06/25/91)
Given, $c = "3/3/3"; $c = tr/\//\//; Why is $c set to 0 and not 2? -- Clay W. Luther cluther@sonne.cnns.unt.edu System Manager Center for Network Neuroscience University of North Texas PO Box 5218 Denton, Texas 76203 817/565-3896,3472 Void where prohibited by law.
Tom Christiansen <tchrist@convex.COM> (06/26/91)
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."