Jump to content

Multisplit: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|Haskell}}: preferred `maybe` to `case Just ... Nothing ...` in fold version.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 240:
separators: '!=' '==' '=' '!='
</pre>
 
=={{header|BBC BASIC}}==
<lang bbcbasic> DIM sep$(2)
Line 323 ⟶ 324:
}</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}}==
Line 413 ⟶ 390:
<pre>a{"!=", (1, 3)}{"==", (3, 5)}b{"=", (6, 7)}{"!=", (7, 9)}c
</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}}==
Line 500 ⟶ 501:
["a",[],"b",[],"c"]
</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}}==
Line 595 ⟶ 607:
5 : c
</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}}==
Line 1,048 ⟶ 1,049:
"c"
</lang>
 
 
=={{header|Kotlin}}==
Line 1,190:
CheckIt
</lang>
 
 
=={{header|Mathematica}}==
Line 1,220 ⟶ 1,219:
{{Out}}
<pre>["a", "{!=}", "", "{==}", "b", "{=}", "", "{!=}"]</pre>
 
 
 
=={{header|Nim}}==
Line 1,265 ⟶ 1,262:
'a' '!=' '' '==' 'b' '=' '' '!=' 'c'
</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}}==
Line 1,561 ⟶ 1,532:
;; => '("a" ("!=") "" ("==") "b" ("=") "" ("!=") "c")
</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}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.