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 method can numify any string you could enter as a L<litteral 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 :ok-test<dd>
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
+
First convert it to a L<Blob|/type/Blob> with L<.encode|/routine/encode>.
330
+
331
+
=begincode :ok-test<dd>
332
+
say "I ❤ 🦋".encode>>.base(16); OUTPUT: «(49 20 E2 9D A4 20 F0 9F A6 8B)»
333
+
=endcode
334
+
335
+
Note that L<.gist|/routine/gist> or L<.raku|/routine/perl> methods are your friends when L<debugging|/programs/01-debugging>:
336
+
337
+
=begincode
338
+
say "I ❤ 🦋".encode.raku; # OUTPUT: «utf8.new(73,32,226,157,164,32,240,159,166,139)»
339
+
say "I ❤ 🦋".encode.gist; # OUTPUT: «utf8:0x<49 20 E2 9D A4 20 F0 9F A6 8B>»
340
+
=endcode
341
+
342
+
=head2String: How can I remove from a string some characters by index
343
+
344
+
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:
345
+
346
+
=begincode :ok-test<dd>
347
+
say '0123456789'.comb[(^* (-) (1..3, 8).flat).keys.sort].join; # OUTPUT: «045679»
348
+
=endcode
349
+
350
+
If the string is large, L<.comb|/routine/comb> can take time. In which case, L<.substr-rw|/routine/substr-rw> is faster:
351
+
352
+
=begincode :ok-test<dd>
353
+
multi postcircumfix:<[- ]> (Str:D $str is copy, +@indices) {
354
+
for @indices.reverse {
355
+
when Int { $str.substr-rw($_,1) = '' }
356
+
when Range { $str.substr-rw($_ ) = '' }
357
+
}
358
+
return $str;
359
+
}
360
+
361
+
say '0123456789'[- 1..3, 8 ]; # OUTPUT: «045679»
362
+
=endcode
363
+
364
+
=head2String: How can I split a string in equal parts
365
+
366
+
L<.comb|/routine/comb> is accepting a L<Int|/type/Int> or a L<Regex|/type/Regex>:
367
+
368
+
=begincode :ok-test<dd>
369
+
.say for 'abcdefghijklmnopqrstuvwxyz'.comb: 8; # OUTPUT: «abcdefghijklmnopqrstuvwxyz»
370
+
.say for 'abcdefg4444hijklmnop4444qrstuvwxyz'.comb: /..\d+../; # OUTPUT: «fg4444hiop4444qr»
0 commit comments