Hi people! Need your help for what i think is a simple problem with a simple solution which i can't figure out. I have a list with phonenumbers coming from a csv file like so:

+12 34567890 +23 45678901 +34 56789012 +45 67890123 +56 78901234

And what i want is simply this:

+12 3456 7890 +23 4567 8901 +34 5678 9012 +45 6789 0123 +56 7890 1234

So basically a whitespace between the 7th and 8th character. Is it possible to do this with regex or should i think of using substr? The phonenumbers are located in column 21. I already did my research and found this topic: How do I insert (not overwrite) into a string? but i'm not sure which i should use or how i can apply this to my case. I've tried: substr ($elements[20], 0, 7) . " " . substr($elements[20], 7); This is my entire code:

if ($#ARGV != 1) {print "usage: input-file output-file \n";exit;} $inputfile=$ARGV[0]; $outputfile=$ARGV[1]; open(INFILE,$inputfile) || die "Bestand niet gevonden :$!\n"; open(OUTFILE,">$outputfile") || die "Bestand niet gevonden :$!\n"; $i = 0; @infile=<INFILE>; foreach ( @infile ) { $infile[$i] =~ s/"//g; # Verwijderd alle " per regel @elements = split(/;/,$infile[$i]); # Create array # Phonenumber clean $elements[20] =~ s/\D//g; # Clear all non-digit #print "$elements[20]\n"; $elements[20] =~ s/(^00|^0)//i; # Remove 00 and 0 #print "$elements[20]\n"; if ($elements[19] =~ m/45/g) # When 45, replace with + +45 { $elements[20] =~ s/^/\+45 /; $elements[19] =~ s/45/xxxx/; #print "$elements[20]\n"; } $elements[20] =~ s/(^450|^45)/\+45 /; # Removes 450/45 and +places +45 #print "$elements[20]"; if ($elements[20] =~ m/^[1-9]/i) # 1 to 9 gets replaced wi +th +45 { $elements[20] =~ s/^/\+45 /; #print "$elements[20]\n"; } if ($elements[20] =~ /^\+45\s{1}\d{9,}$/) { # More then 8 digit +s, nr to long $elements[25] = 'TE LANG NR'; #print "$elements[24]\n"; } if ($elements[20] =~ /^\+45\s{1}\d{1,7}$/) { # Less then 8, to +short $elements[25] = 'TE KORT NR'; #print "$elements[24]\n"; } if (!($elements[20])) { $elements[25] = 'GEEN NR BEKEND'; # Empty field = no number } if ($elements[20] =~ m/^.{12}/ substr ($elements[20], 0, 7) . " " . substr($elements[20], 7); + # End phonenumber clean @elements = join(";",@elements); # Add ';' to all elements print OUTFILE "@elements"; $i = $i+1; } close(INFILE); close(OUTFILE);

How do i fit the substr function in there? Oh fyi, i'm new to perl;)

In reply to Add whitespace in middle of string (substr?) by Janwhatever

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.