in reply to (jcwren) RE: Counting Characters
in thread Counting Characters
As far as I can tell, you can't use the match operator to return the length. The closest I came up with is a variation of Ozymandias' response:
In the Perl Cookbook, it has a recipe for finding the nth occurence of a match, but that also uses a loop. If you could enumerate matches with a straight regex, you could construct a well-defined regex and skip the loop in the Cookbook.$string = "This is a test\nMore Test"; $len++ for ($string =~ /./sg); print $len;
Believe me, I tried :) If any monks would like to tackle this, I'd love to be proven wrong (and I'm sure it would be something ridiculously simple).
Update: And in my quest to come up with horribly unoptimized code:
Or you can use this beauty (no, I'm not serious):$_ = "This is a test\nMore Test"; $len = (split //); print $len;
No, I don't suggest using them. I just had to toss it out because no one else had mentioned it :)$string = "This is a test\nMore Test"; $len = (grep /./s, (@chars = split //, $string));
|
|---|