I am trying to figure out how to count any and all non-whitespace characters in a string such that in the example below I would get an output of 4.
my $str = 'A b C $';
my $cnt =~ /\S/g;
print "Char Count: $cnt\n";
This Is there a Perl shortcut to count the number of matches in a string? does not answer my question.
perl -e 'my $str = "A b C"; my $cnt = () = $str =~ /\./gi; print "Chs: $cnt\n";'
Chs: 0
Someone keeps wanting to say that this question is a duplicate of this question: Is there a Perl shortcut to count the number of matches in a string?
However, the other question references how to match a specific character, in their case I believe it's a dot. I looked at many other other examples that seek to match a specific character and could not get them to work to match all characters except whitespace.
This does not answer my question.
$string = "ThisXlineXhasXsomeXx'sXinXit";
$count = ($string =~ tr/X//);
print "There are $count X characters in the string";
This question: What do \S, \W, \D stand for in regex? merely defines various Perl wildcard operators--one of which (the \S operator) I attempted to employ in my original question to no avail. It does not however demonstrate how one actually employs one of those operators in order to obtain the count of all non-whitespace characters in a string.