Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: total noob trying to format a phone list

by jeffa (Bishop)
on Apr 24, 2015 at 18:05 UTC ( [id://1124590]=note: print w/replies, xml ) Need Help??


in reply to trying to format a list

And my comments are this: run this code and ask questions :)

use strict; use warnings; my( $area_code, $extension ) = ( 333, 321 ); while (<DATA>) { my ($number) = $_ =~ /\s([0-9()-]+)/; $number =~ s/\D//g; if (length( $number ) == 4) { $number = $area_code . $extension . $number; } elsif (length( $number ) == 7) { $number = $area_code . $number; } elsif (length( $number ) == 10) { # do nothing } else { warn "could not parse $number\n"; next; } $number =~ s/(\d{3})(\d{3})(\d{4})/($1)-$2-$3/; $_ =~ s/([0-9()-]+)/$number/; print; } __DATA__ Black, Joe 0987 Smith, Sue 0534 Brown, Andy 587-0986 Smith, Pam (615)-895-1010
What this code essentially does is pull out things that look like phone numbers from each line. Then removes non-digits and checks the length of what is left. Based on that length, you can determine whether you need to add your default area code (333) and/or the default extension (321). Then you format the final number and substitute that back into your original line.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
trying to format list
by sonikd (Initiate) on Apr 24, 2015 at 18:27 UTC
    thanks so much!!! i will try this and revert back. needs to be alphabetical and i need to use the same area code and exchange prefix unless a number has a different prefix, in which case i then need to filter that exchange prefix.

      You are welcome. You can sort within this code, or you could just use sort utility from the shell:

      $ perl 1124590.pl Black, Joe (333)-321-0987 Smith, Sue (333)-321-0534 Brown, Andy (333)-587-0986 Smith, Pam (615)-895-1010 $ perl 1124590.pl | sort Black, Joe (333)-321-0987 Brown, Andy (333)-587-0986 Smith, Pam (615)-895-1010 Smith, Sue (333)-321-0534

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1124590]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found