You could also use unpack for this, preferably in conjuction with sprintf:
#!/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

Update: Added last example.

--
John.


In reply to Re: Format of number by template by jmcnamara
in thread Format of number by template by nite_man

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.