In Perl 5 we can write
my @things = $text =~ /thing/g;
And $things
in scalar context is number of non-overlapping occurrences of substring thing
in string $text
.
How to do this in Perl 6?
You can do it like this:
my $text = 'thingthingthing'
my @things = $text ~~ m:g/thing/;
say +@things; # 3
~~
matches the left side against the right side, m:g
makes the test return a List[Match]
containing all the results.
I found solution on RosettaCode
.
http://rosettacode.org/wiki/Count_occurrences_of_a_substring#Perl_6
say '01001011'.comb(/1/).elems; #prints 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With