Multisplit: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: preferred `maybe` to `case Just ... Nothing ...` in fold version.) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 240: | Line 240: | ||
separators: '!=' '==' '=' '!=' |
separators: '!=' '==' '=' '!=' |
||
</pre> |
</pre> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
<lang bbcbasic> DIM sep$(2) |
<lang bbcbasic> DIM sep$(2) |
||
Line 323: | Line 324: | ||
}</lang> |
}</lang> |
||
{{out}}<lang>a{!=}{==}b{=}{!=}c</lang> |
{{out}}<lang>a{!=}{==}b{=}{!=}c</lang> |
||
=={{header|C++}}== |
|||
using the Boost library tokenizer! |
|||
<lang cpp>#include <iostream> |
|||
#include <boost/tokenizer.hpp> |
|||
#include <string> |
|||
int main( ) { |
|||
std::string str( "a!===b=!=c" ) , output ; |
|||
typedef boost::tokenizer<boost::char_separator<char> > tokenizer ; |
|||
boost::char_separator<char> separator ( "==" , "!=" ) , sep ( "!" ) ; |
|||
tokenizer mytok( str , separator ) ; |
|||
tokenizer::iterator tok_iter = mytok.begin( ) ; |
|||
for ( ; tok_iter != mytok.end( ) ; ++tok_iter ) |
|||
output.append( *tok_iter ) ; |
|||
tokenizer nexttok ( output , sep ) ; |
|||
for ( tok_iter = nexttok.begin( ) ; tok_iter != nexttok.end( ) ; |
|||
++tok_iter ) |
|||
std::cout << *tok_iter << " " ; |
|||
std::cout << '\n' ; |
|||
return 0 ; |
|||
}</lang> |
|||
{{out}} |
|||
<PRE>a b c</PRE> |
|||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
Line 413: | Line 390: | ||
<pre>a{"!=", (1, 3)}{"==", (3, 5)}b{"=", (6, 7)}{"!=", (7, 9)}c |
<pre>a{"!=", (1, 3)}{"==", (3, 5)}b{"=", (6, 7)}{"!=", (7, 9)}c |
||
</pre> |
</pre> |
||
=={{header|C++}}== |
|||
using the Boost library tokenizer! |
|||
<lang cpp>#include <iostream> |
|||
#include <boost/tokenizer.hpp> |
|||
#include <string> |
|||
int main( ) { |
|||
std::string str( "a!===b=!=c" ) , output ; |
|||
typedef boost::tokenizer<boost::char_separator<char> > tokenizer ; |
|||
boost::char_separator<char> separator ( "==" , "!=" ) , sep ( "!" ) ; |
|||
tokenizer mytok( str , separator ) ; |
|||
tokenizer::iterator tok_iter = mytok.begin( ) ; |
|||
for ( ; tok_iter != mytok.end( ) ; ++tok_iter ) |
|||
output.append( *tok_iter ) ; |
|||
tokenizer nexttok ( output , sep ) ; |
|||
for ( tok_iter = nexttok.begin( ) ; tok_iter != nexttok.end( ) ; |
|||
++tok_iter ) |
|||
std::cout << *tok_iter << " " ; |
|||
std::cout << '\n' ; |
|||
return 0 ; |
|||
}</lang> |
|||
{{out}} |
|||
<PRE>a b c</PRE> |
|||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
Line 500: | Line 501: | ||
["a",[],"b",[],"c"] |
["a",[],"b",[],"c"] |
||
</pre> |
</pre> |
||
=={{header|F_Sharp|F#}}== |
|||
If we ignore the "Extra Credit" requirements and skip 'ordered separators' condition (i.e. solving absolute different task), this is exactly what one of the overloads of .NET's <code>String.Split</code> method does. Using F# Interactive: |
|||
<lang fsharp>> "a!===b=!=c".Split([|"=="; "!="; "="|], System.StringSplitOptions.None);; |
|||
val it : string [] = [|"a"; ""; "b"; ""; "c"|] |
|||
> "a!===b=!=c".Split([|"="; "!="; "=="|], System.StringSplitOptions.None);; |
|||
val it : string [] = [|"a"; ""; ""; "b"; ""; "c"|]</lang> |
|||
<code>System.StringSplitOptions.None</code> specifies that empty strings should be included in the result. |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Line 595: | Line 607: | ||
5 : c |
5 : c |
||
</pre> |
</pre> |
||
=={{header|F_Sharp|F#}}== |
|||
If we ignore the "Extra Credit" requirements and skip 'ordered separators' condition (i.e. solving absolute different task), this is exactly what one of the overloads of .NET's <code>String.Split</code> method does. Using F# Interactive: |
|||
<lang fsharp>> "a!===b=!=c".Split([|"=="; "!="; "="|], System.StringSplitOptions.None);; |
|||
val it : string [] = [|"a"; ""; "b"; ""; "c"|] |
|||
> "a!===b=!=c".Split([|"="; "!="; "=="|], System.StringSplitOptions.None);; |
|||
val it : string [] = [|"a"; ""; ""; "b"; ""; "c"|]</lang> |
|||
<code>System.StringSplitOptions.None</code> specifies that empty strings should be included in the result. |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 1,048: | Line 1,049: | ||
"c" |
"c" |
||
</lang> |
</lang> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 1,190: | Line 1,190: | ||
CheckIt |
CheckIt |
||
</lang> |
</lang> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
Line 1,220: | Line 1,219: | ||
{{Out}} |
{{Out}} |
||
<pre>["a", "{!=}", "", "{==}", "b", "{=}", "", "{!=}"]</pre> |
<pre>["a", "{!=}", "", "{==}", "b", "{=}", "", "{!=}"]</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Line 1,265: | Line 1,262: | ||
'a' '!=' '' '==' 'b' '=' '' '!=' 'c' |
'a' '!=' '' '==' 'b' '=' '' '!=' 'c' |
||
</pre> |
</pre> |
||
=={{header|Perl 6}}== |
|||
{{Works with|rakudo|2015-11-29}} |
|||
<lang perl6>sub multisplit($str, @seps) { $str.split(/ ||@seps /, :v) } |
|||
my @chunks = multisplit( 'a!===b=!=c==d', < == != = > ); |
|||
# Print the strings. |
|||
say @chunks».Str.perl; |
|||
# Print the positions of the separators. |
|||
for grep Match, @chunks -> $s { |
|||
say " $s from $s.from() to $s.to()"; |
|||
}</lang> |
|||
{{out}} |
|||
<pre>("a", "!=", "", "==", "b", "=", "", "!=", "c", "==", "d") |
|||
!= from 1 to 3 |
|||
== from 3 to 5 |
|||
= from 6 to 7 |
|||
!= from 7 to 9 |
|||
== from 10 to 12</pre> |
|||
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). |
|||
As it happens, with the task's specified list of separators, it doesn't make any difference. |
|||
<p> |
|||
Perl 6 automatically returns Match objects that will stringify to the matched pattern, but can also be interrogated for their match positions, as illustrated above by post-processing the results two different ways. |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,561: | Line 1,532: | ||
;; => '("a" ("!=") "" ("==") "b" ("=") "" ("!=") "c") |
;; => '("a" ("!=") "" ("==") "b" ("=") "" ("!=") "c") |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{Works with|rakudo|2015-11-29}} |
|||
<lang perl6>sub multisplit($str, @seps) { $str.split(/ ||@seps /, :v) } |
|||
my @chunks = multisplit( 'a!===b=!=c==d', < == != = > ); |
|||
# Print the strings. |
|||
say @chunks».Str.perl; |
|||
# Print the positions of the separators. |
|||
for grep Match, @chunks -> $s { |
|||
say " $s from $s.from() to $s.to()"; |
|||
}</lang> |
|||
{{out}} |
|||
<pre>("a", "!=", "", "==", "b", "=", "", "!=", "c", "==", "d") |
|||
!= from 1 to 3 |
|||
== from 3 to 5 |
|||
= from 6 to 7 |
|||
!= from 7 to 9 |
|||
== from 10 to 12</pre> |
|||
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). |
|||
As it happens, with the task's specified list of separators, it doesn't make any difference. |
|||
<p> |
|||
Perl 6 automatically returns Match objects that will stringify to the matched pattern, but can also be interrogated for their match positions, as illustrated above by post-processing the results two different ways. |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |