Count occurrences of a substring: Difference between revisions
Content added Content deleted
Line 2,600: | Line 2,600: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang perl6>sub count-substring($big,$little) { +$big.comb: |
<lang perl6>sub count-substring($big, $little) { +$big.comb: / :r $little / } |
||
say count-substring("the three truths","th"); # 3 |
say count-substring("the three truths", "th"); # 3 |
||
say count-substring("ababababab","abab"); # |
say count-substring("ababababab", "abab"); # 2 |
||
say count-substring(123123123,12); # 3</lang> |
say count-substring(123123123, 12); # 3</lang> |
||
The <tt> |
The <tt>:r</tt> adverb makes the regex "ratchet forward" and skip any overlapping matches. <tt>.comb</tt> - when given a <tt>Regex</tt> as an argument - returns instances of that substring. Also, prefix <tt>+</tt> forces numeric context in Raku (it's a no-op in Perl 5). For the built in listy types that is the same as calling <tt>.elems</tt> method. One other style point: we now tend to prefer hyphenated names over camelCase. |
||
=={{header|Red}}== |
=={{header|Red}}== |