DB<150> sprintf "%d-%d-%d-%d", ('1234567890123456' =~ /(....)/g) => "1234-5678-9012-3456"
or this
DB<151> sprintf "%d%d%d%d-%d%d%d%d-%d%d%d%d-%d%d%d%d", split //, '12 +34567890123456' => "1234-5678-9012-3456"
But I'd rather prefer this
DB<153> join "-", ('1234567890123456' =~ /(....)/g) => "1234-5678-9012-3456"
TIMTOWTDI ... depends if you always make sure that the input is 16 characters long.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!
kennethk++ msged me that leading 0 are a problem in the first approach
DB<160> sprintf "%d-%d-%d-%d", ('0123456789012345' =~ /(....)/g) => "123-4567-8901-2345"
this would fix it
DB<161> sprintf "%04d-%04d-%04d-%04d", ('0123456789012345' =~ /(.... +)/g) => "0123-4567-8901-2345"
but honestly, I doubt I would stick with this approach.
In reply to Re: Formatting with sprintf
by LanX
in thread Formatting with sprintf
by citabriabob
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |