#!/usr/bin/perl -wl use strict; my $str = 123456789012345; print join "-", "No", unpack("a3 a5 a7", $str); # Or slightly more flexible: # Zero pad the string to the required length $str = sprintf "%015s", $str; my $template = "###-#####-#######"; $template =~ s/-/ /g; $template =~ s/(#+)/'a' . length $1/eg; # Template is now "a3 a5 a7" print join "-", "No", unpack($template, $str); # You could even automate it a little more as follows: my $digits; $str = 12345; $template = "###-#####-#######"; $digits = $template =~ tr/#//; $template =~ s/-/ /g; $template =~ s/(#+)/'a' . length $1/eg; $str = sprintf "%0*s", $digits, $str; print join "-", "No", unpack($template, $str); __END__ Prints: No-123-45678-9012345 No-123-45678-9012345 No-000-00000-0012345