# ðŸ“˜ Separating groups of digits using Perl 6

Put commas between the three-digit groups in a big number.

The task is to print an integer number, for example, 1234567890,in the form of 1,234,567,890.

Here is a possible solution that uses a lot of Perl 6 facilities:

`\$n ~~ s/<?after \d> (\d ** 3)+ \$/{\$0.map(',' ~ *).join}/;`

On the top level, weâ€™ve got a substitutionÂ `s///`. The pattern is anchored to the end of the string:Â `s/...\$//`. From the end of the string, groups of three digits are searched:Â `(\d ** 3)+`. A singleÂ `\d` matches a digit, and theÂ `**` quantifier requires exactly three of them. The second quantifier,Â `+`, allows more than one such groups.

A dot may only follow at least one digit. To avoid converting six-digit numbers to something likeÂ .123.456, a look-behind assertionÂ `<?after \d>` is inserted, which insists that there is a digit before the first group of three digits.Â The whole replacement only happens if there are more than four digits in the original number.

The second part of the substitution is an executable code:Â `s//{...}/`. After a successful match, the three-digit groups appear in the match objectÂ `\$0`. Because of theÂ `+` quantifier, you can treat this object as an array.

First, each element is translated to a string with a comma in front of it:Â `map(',' ~ *)`. Using theÂ `*` character creates a `WhateverCode` block, which is equivalent toÂ `{',' ~ \$_}`. It is also possible to rewrite the map operation using the string interpolation:Â `map({",\$_"})`.

Finally, transformed parts are joined together, using theÂ `join` method call. TheÂ `\$n` variable now contains a string with the desired result.