Multisplit: Difference between revisions
m
→{{header|Raku}}: fewer parens, nicer output formatting
m (→{{header|J}}: clearer result format) |
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: fewer parens, nicer output formatting) |
||
Line 1,719:
=={{header|Raku}}==
(formerly Perl 6)
▲<lang perl6>sub multisplit($str, @seps) { $str.split(/ ||@seps /, :v) }
my @chunks = multisplit
# Print the strings.
Line 1,729 ⟶ 1,728:
# Print the positions of the separators.
for grep Match, @chunks -> $s {
say "
}</lang>
{{out}}
<pre>("a", "!=", "", "==", "b", "=", "", "!=", "c", "==", "d")
Using the array <tt>@seps</tt> in a pattern automatically does alternation.
By default this would do longest-term matching (that is, <tt>|</tt> semantics), but we can force it to do left-to-right matching by embedding the array in a short-circuit alternation (that is, <tt>||</tt> semantics).
|