And what i want is simply this:+12 34567890 +23 45678901 +34 56789012 +45 67890123 +56 78901234
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:+12 3456 7890 +23 4567 8901 +34 5678 9012 +45 6789 0123 +56 7890 1234
How do i fit the substr function in there? Oh fyi, i'm new to perl;)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);
In reply to Add whitespace in middle of string (substr?) by Janwhatever
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |