You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/faq.pod6
+71Lines changed: 71 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -299,6 +299,77 @@ dependencies within a single compilation unit (e.g., file) are possible
299
299
through stubbing. Therefore another possible solution is to move
300
300
classes into the same compilation unit.
301
301
302
+
303
+
=head1Common operations
304
+
305
+
=head2String: How can I parse and get a L<number|/language/numerics> from a L<string|/language/type/Str>
306
+
307
+
Use the L<+ prefix|/language/operators#prefix_+>:
308
+
309
+
=begincode :ok-test<dd>
310
+
say "42.123456789123456789"; # OUTPUT: «42.123456789123456789»
311
+
say +"42.4e-2"; # OUTPUT: «0.424»
312
+
=endcode
313
+
314
+
This example of L<contextualization|/language/contexts> can numify any string you could enter as a L<literal number|/language/syntax#Number_literals>.
315
+
L<val|/routine/val> routine converts it to L<allomorph|/language/glossary#Allomorph>.
316
+
L<unival|/routine/unival> routine converts one unicode codepoint.
317
+
318
+
=head2String: How can I check if a string contains a substring and if so, how can I get indices of matches
319
+
320
+
Use L<.contains|/type/Str#method_contains> or L<.indices|/type/Str#method_indices>:
321
+
322
+
=begincode
323
+
"az and az and az again".contains("az"); # OUTPUT: «True»
324
+
"az and az and az again".indices("az"); # OUTPUT: «(0 7 14)»
325
+
=endcode
326
+
327
+
=head2String: How can I get the hexadecimal representation of a string
328
+
329
+
To get an hexadecimal list of each byte of a string (i.e. hex encoder),
330
+
first convert the string to a L<Blob|/type/Blob> with L<.encode|/routine/encode>.
331
+
332
+
=begincode
333
+
say "I ❤ 🦋".encode>>.base(16); OUTPUT: «(49 20 E2 9D A4 20 F0 9F A6 8B)»
334
+
=endcode
335
+
336
+
Note that L<.gist|/routine/gist> or L<.raku|/routine/perl> methods can be useful for variable introspection:
337
+
338
+
=begincode
339
+
say "I ❤ 🦋".encode.raku; # OUTPUT: «utf8.new(73,32,226,157,164,32,240,159,166,139)»
340
+
say "I ❤ 🦋".encode.gist; # OUTPUT: «utf8:0x<49 20 E2 9D A4 20 F0 9F A6 8B>»
341
+
=endcode
342
+
343
+
=head2String: How can I remove from a string some characters by index
344
+
345
+
Use L<.comb|/routine/comb> to transform it to a L<Seq|/type/Seq>, then the L<(-) infix|/language/operators#infix_(-),_infix_\\> to remove the unwanted indices:
346
+
347
+
=begincode
348
+
say '0123456789'.comb[(^* (-) (1..3, 8).flat).keys.sort].join; # OUTPUT: «045679»
349
+
=endcode
350
+
351
+
If the string is large, L<.comb|/routine/comb> can take time. In which case, L<.substr-rw|/routine/substr-rw> is faster:
352
+
353
+
=begincode
354
+
multi postcircumfix:<[- ]> (Str:D $str is copy, +@indices) {
355
+
for @indices.reverse {
356
+
when Int { $str.substr-rw($_,1) = '' }
357
+
when Range { $str.substr-rw($_ ) = '' }
358
+
}
359
+
return $str;
360
+
}
361
+
362
+
say '0123456789'[- 1..3, 8 ]; # OUTPUT: «045679»
363
+
=endcode
364
+
365
+
=head2String: How can I split a string in equal parts
366
+
367
+
L<.comb|/routine/comb> is accepting an optional L<Int|/type/Int>:
368
+
369
+
=begincode
370
+
.say for 'abcdefghijklmnopqrstuvwxyz'.comb: 8; # OUTPUT: «abcdefghijklmnopqrstuvwxyz»
0 commit comments