Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: trying to format a phone list

by aaron_baugher (Curate)
on Apr 28, 2015 at 02:30 UTC ( [id://1124948]=note: print w/replies, xml ) Need Help??


in reply to trying to format a list

This is a fairly complex task for a "total noob." You need to know how to:

  • Open files for reading and writing
  • Read a file line-by-line
  • Parse values from a line by one method or another
  • Insert values based on what's there
  • Sort values
  • Print out the sorted and changed values

All those topics would be scattered through several chapters of any book on learning Perl, so don't feel bad if you struggle with it for a while. Start at the top of the list and work your way down: learn how to read and write files, how to loop through a file line-by-line, and so on, and you'll get there.

Just for fun, here's a one-liner solution that leverages the Unix sort utility so I don't have to do it in Perl. When you can figure out what this is doing, you won't be a noob anymore.

$ cat 1124577.txt Black, Joe 0987 Smith, Sue 0534 Brown, Andy 587-0986 Galt, John (212)-555-1212 $ perl -ne 'print if s|^(.*\S)\s+(\(\d\d\d\)-)?(\d\d\d-)?(\d\d\d\d)|$1 + . " " . ($2?$2:"(333)-") . ($3?$3:"321-") . $4|e' 1124577.txt | sort Black, Joe (333)-321-0987 Brown, Andy (333)-587-0986 Galt, John (212)-555-1212 Smith, Sue (333)-321-0534

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found