TIMTOWTDI. Used substr() and simple functions.
#!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>){ chomp($line); $line = "0" . $line while length($line) < 9; my $first = substr($line, 0, 3); my $second= substr($line, 3, 2); my $third = substr($line, 5, 4); my $str = $first . '-' . $second . '-' . $third; print $str . "\n"; } __DATA__ 458430764 453453214 462634878 453990002 462755714 631036456 466917461 467172570 454691673 258611036 42

I have added an extra check, just in case it is a smaller number and not the 9 numbers, we put "0" in front of our read $line. but know that Perl handles the numbers in the files as strings, only when you start doing mathematical operations with them, then Perl automatically handles them as numbers. You can also just print a warning and skip, like so:

if(length($line) != 9){ warn "Bad data read (at line $.): '$line' length is not correct.\n"; next; # skip processing this line } if ($line !~ /^\d+$/){ warn "Bad data read (at line $.): '$line' not numerical.\n"; next; # skip processing this line }

edit: added latter check. Assuming we have positive integer numbers only (no E or negative numbers, no floats)


In reply to Re: Add - in between number in a file by FreeBeerReekingMonk
in thread Add - in between number in a file by notoperlbegineer

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.