@@ -177,42 +177,86 @@ Examples:
177
177
178
178
= head2 routine split
179
179
180
- multi sub split( Str:D $delimiter, Str:D $input, $limit = Inf, :$all) returns Positional
181
- multi sub split(Regex:D $delimiter, Str:D $input, $limit = Inf, :$all) returns Positional
182
- multi method split(Str:D $input: Str:D $delimiter, $limit = Inf, :$all) returns Positional
183
- multi method split(Str:D $input: Regex:D $delimiter, $limit = Inf, :$all) returns Positional
180
+ multi sub split( Str:D $delimiter, Str:D $input, $limit = Inf,
181
+ :$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
182
+ multi sub split(Regex:D $delimiter, Str:D $input, $limit = Inf,
183
+ :$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
184
+ multi sub split(List:D $delimiters, Str:D $input, $limit = Inf,
185
+ :$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
186
+ multi method split(Str:D: Str:D $delimiter, $limit = Inf,
187
+ :$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
188
+ multi method split(Str:D: Regex:D $delimiter, $limit = Inf,
189
+ :$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
190
+ multi method split(Str:D: List:D $delimiters, $limit = Inf,
191
+ :$skip-empty, :$v, :$k, :$kv, :$p) returns Positional
184
192
185
193
Usage:
186
194
187
- split( DELIMITER, STRING [, LIMIT] [, :all])
188
- split( /PATTERN/, STRING [, LIMIT] [, :all])
189
- STRING.split( DELIMITER [, LIMIT] [, :all])
190
- STRING.split( /PATTERN/ [, LIMIT] [, :all])
195
+ split( DELIMITER, STRING [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
196
+ split( /PATTERN/, STRING [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
197
+ split( DELIMITERS, STRING [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
198
+ STRING.split( DELIMITER [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
199
+ STRING.split( /PATTERN/ [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
200
+ STRING.split( DELIMITERS [,LIMIT] [,:v] [,:k] [,:kv] [,:p] [,:skip-empty] )
191
201
192
202
Splits a string up into pieces based on delimiters found in the string.
193
203
194
204
If C < DELIMITER > is a string, it is searched for literally and not treated
195
- as a regex.
205
+ as a regex. If C < DELIMITER > is the empty string, it effectively returns all
206
+ characters of the string separately (plus an empty string at the begin and at
207
+ the end). If C < PATTERN > is a regular expression, then that will be used
208
+ to split up the string. If C < DELIMITERS > is a list, then all of its elements
209
+ will be considered a delimiter (either a string or a regular expression) to
210
+ split the string on.
196
211
197
- If the named parameter C < :all > is passed, the matches from C < DELIMITER >
198
- are included in the result list.
212
+ The optional C < LIMIT > indicates in how many segments the string should be
213
+ split, if possible. It defaults to B < Inf > (or B < * > , whichever way you look at
214
+ it), which means "as many as possible".
199
215
200
- Note that unlike in Perl 5, empty chunks are not removed from the result list.
201
- If you want that behavior, consider using L < comb > instead.
216
+ A number of optional named parameters can be specified, which alter the
217
+ result being returned. The C < :v > , C < :k > , C < :kv > and C < :p > named parameters
218
+ all perform a special action with regards to the delimiter found.
202
219
203
- = comment TODO Describe behavior of LIMIT
220
+ = item :skip-empty
221
+
222
+ If specified, do not return empty strings before or after a delimiter.
223
+
224
+ = item :v
225
+
226
+ Also return the delimiter. If the delimiter was a regular expression, then
227
+ this will be the associated C < Match > object. Since this stringifies as the
228
+ delimiter string found, you can always assume it is the delimiter string if
229
+ you're not interested in further information about that particular match.
230
+
231
+ = item :k
232
+
233
+ Also return the B < index > of the delimiter. Only makes sense if a list of
234
+ delimiters was specified: in all other cases, this will be B < 0 > .
235
+
236
+ = item :kv
237
+
238
+ Also return both the B < index > of the delimiter, as well as the delimiter.
239
+
240
+ = item :p
241
+
242
+ Also return the B < index > of the delimiter and the delimiter as a C < Pair > .
204
243
205
244
Examples:
206
245
207
246
= begin code
208
- say split(';', "a;b;c").perl; # ("a", "b", "c").list
209
- say split(';', "a;b;c", :all).perl; # (("a", ";"), ("b", ";"), "c").list
210
- say split(';', "a;b;c", 2).perl; # ("a", "b;c").list
211
- say split(';', "a;b;c", 2, :all).perl; # (("a", ";"), "b;c").list
212
-
213
- say split(';', "a;b;c,d").perl; # ("a", "b", "c,d").list
214
- say split(/\;/, "a;b;c,d").perl; # ("a", "b", "c,d").list
215
- say split(/<[;,]>/, "a;b;c,d").perl; # ("a", "b", "c", "d").list
247
+ say split(";", "a;b;c") # (a b c)
248
+ say split(";", "a;b;c", :v) # (a ; b ; c)
249
+ say split(";", "a;b;c", 2) # (a b;c)
250
+ say split(";", "a;b;c", 2, :v) # (a ; b;c)
251
+ say split(";", "a;b;c,d") # (a b c,d)
252
+ say split(/\;/, "a;b;c,d") # (a b c,d)
253
+ say split(<; ,>, "a;b;c,d") # (a b c d)
254
+ say split(/<[;,]>/, "a;b;c,d") # (a b c d)
255
+ say split(<; ,>, "a;b;c,d", :k) # (a 0 b 0 c 1 d)
256
+ say split(<; ,>, "a;b;c,d", :kv) # (a 0 ; b 0 ; c 1 , d)
257
+
258
+ say "abcde".split("") # ( a b c d e )
259
+ say "abcde".split("",:skip-empty) # (a b c d e)
216
260
= end code
217
261
218
262
= head2 routine comb
0 commit comments