Skip to content

Instantly share code, notes, and snippets.

@dha
Created October 2, 2015 21:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dha/e69e8daa6c6519c71060 to your computer and use it in GitHub Desktop.
=begin pod
=head2 routine split
Definded as:
multi sub split( Str:D $delimiter, Str:D $input, $limit = Inf, :$all) returns Positional
multi sub split(Regex:D $delimiter, Str:D $input, $limit = Inf, :$all) returns Positional
multi method split(Str:D $input: Str:D $delimiter, $limit = Inf, :$all) returns Positional
multi method split(Str:D $input: Regex:D $delimiter, $limit = Inf, :$all) returns Positional
Usage:
split( DELIMITER, EXPR [, LIMIT] [, :all])
split( PATTERN, EXPR [, LIMIT] [, :all])
EXPR.split( DELIMITER [, LIMIT] [, :all])
EXPR.split( PATTERN [, LIMIT] [, :all])
Splits a string up into pieces based on delimiters found in the string.
If C<$delimiter> is a string, it is searched for literally and not treated
as a regex.
If the named parameter C<:all> is passed, the matches from C<$delimiter>
are included in the result list.
Note that unlike in Perl 5, empty chunks are not removed from the result list.
If you want that behavior, consider using L<comb> instead.
Examples:
=begin code
say split(';', "a;b;c").perl; # ("a", "b", "c").list
say split(';', "a;b;c", :all).perl; # (("a", ";"), ("b", ";"), "c").list
say split(';', "a;b;c", 2).perl; # ("a", "b;c").list
say split(';', "a;b;c", 2, :all).perl; # (("a", ";"), "b;c").list
say split(';', "a;b;c,d").perl; # ("a", "b", "c,d").list
say split(/\;/, "a;b;c,d").perl; # ("a", "b", "c,d").list
say split(/<[;,]>/, "a;b;c,d").perl; # ("a", "b", "c", "d").list
=end code
=end pod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment