in reply to oneliner to count the number binary digits in this string 1011111

LanX's use of the tr/// operator (see perlop) is best (simplest, fastest) for simple counting.

For counting in cases of more complex matching, m// (perlop) can be used:

Win8 Strawberry 5.8.9.5 (32) Sun 03/14/2021 17:12:13 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $s = '1010011 f1e f0o g0e'; my $n =()= $s =~ m{ (?<= [fg]) [01] (?= [oe]) }xmsg; print "$n [01] in '$s' \n"; ^Z 3 [01] in '1010011 f1e f0o g0e'
See goatse (don't ask) for a discussion of the =()= "operator."


Give a man a fish:  <%-{-{-{-<