Re: total noob trying to format a phone list
by jeffa (Bishop) on Apr 24, 2015 at 18:05 UTC
|
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)
| [reply] [d/l] |
|
|
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.
| [reply] |
|
|
$ 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)
| [reply] [d/l] |
Re: total noob trying to format a phone list
by stevieb (Canon) on Apr 24, 2015 at 18:00 UTC
|
There wasn't much input you supplied, so I made assumptions that all numbers will be in either one or two of those formats. I'll leave it up to you to hack as necessary to handle other formats (I left the else-if open).
Also, I'll leave the exercise of changing the input file from __DATA__ to an actual file, but the open for writing line should help
#!/usr/bin/perl
use warnings;
use strict;
open my $fh, '+>', 'output.txt'
or die("Can't open the damn file!: $!");
while(my $line = <DATA>){
chomp $line;
my ($name, $num) = split(/\s+(?=\d)/, $line);
# some lines have whitespace after the num
$num =~ s/\s+//g;
if ($num =~ /^\d{4}$/){
$num = "(333)-321-$num";
}
elsif ($num =~ /^\d{3}-\d{4}$/){
$num = "(333)-$num";
}
print $fh "$name $num\n";
}
__DATA__
Black, Joe 0987
Smith, Sue 0534
Brown, Andy 587-0986
Output:
$ cat output.txt
Black, Joe (333)-321-0987
Smith, Sue (333)-321-0534
Brown, Andy (333)-587-0986
-stevieb | [reply] [d/l] [select] |
|
|
use warnings;
use strict;
open my $fh, '+>', 'output.txt'
or die("Can't open the damn file!: $!");
while(my $line = 'mytextfile');
{
chomp $line;
my ($name, $num) = split(/\s+(?=\d)/, $line)
# some lines have whitespace after the num
$num =~ s/\s+//g;
if ($num =~ /^\d{4}$/){
$num = "(333)-447-$num";
}
elsif ($num =~ /^\d{3}-\d{4}$/){
$num = "(333)-$num";
}
print $fh "$name $num\n";
}
| [reply] [d/l] |
|
|
It looks as thought you moved much closer to your target.
BUT, you didn't give us all the errors nor is what you did post, verbatim. Redacting messages when posting simply makes it harder for us to spot where you need help.
Below, with my comments added and the corrections in place, is the code you posted with all the messages generated in the course of running (using the test, perl -c ....) fixing, and running again, until testing produced the heartwarming "syntax OK (BTW, that does NOT mean the code is free of logical errors, nor that it will do what you intended).
use warnings;
use strict;
open my $fh, '+>', 'output.txt'
or die("Can't open the damn file!: $!");
while(my $line == 'mytextfile') # semi-colon removed(run1) & (run2
+)equity test to '=='
{ # extra empty line removed: HazNav
+ (not an error)
chomp $line;
my ($name, $num) = split(/\s+(?=\d)/, $line); # missing semi-col
+on inserted (run 1)
# some lines have whitespace after the num
$num =~ s/\s+//g;
if ($num =~ /^\d{4}$/){
$num = "(333)-447-$num";
}
elsif ($num =~ /^\d{3}-\d{4}$/){
$num = "(333)-$num";
}
print $fh "$name $num\n";
}
Below is the verbatim output when I first ran your code exactly as posted (my comments and corrections for both runs are included).
C:>perl -c delete_me.pl
Scalar found where operator expected at delete_me.pl line 16, near ")
# some lines have whitespace after the num
$num"
(Missing operator before $num?)
syntax error at delete_me.pl line 8, near ");"
syntax error at delete_me.pl line 16, near ")
# some lines have whitespace after the num
$num "
Global symbol "$num" requires explicit package name at delete_me.pl li
+ne 16.
Global symbol "$num" requires explicit package name at delete_me.pl li
+ne 18.
Global symbol "$num" requires explicit package name at delete_me.pl li
+ne 19.
Global symbol "$num" requires explicit package name at delete_me.pl li
+ne 19.
Global symbol "$num" requires explicit package name at delete_me.pl li
+ne 21.
Global symbol "$num" requires explicit package name at delete_me.pl li
+ne 22.
Global symbol "$num" requires explicit package name at delete_me.pl li
+ne 22.
delete_me.pl had compilation errors.
... and, when those marked "run 1" were fixed, a second iteration, "run 2," told me:
Found = in conditional, should be == at D:\_Perl_\pl_test\delete_me.pl
+ line 9.
which was fixed by using "==" in the equity test at Ln 9.
Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
- code
- verbatim error and/or warning messages
- a coherent explanation of what "doesn't work actually means.
| [reply] [d/l] [select] |
|
|
ww pointed out some basic troubleshooting steps, but at a glance after you used my code, I'll elaborate on his thorough post (thanks ww): you made changes that won't work. First, you have a semi-colon after the while() statement, and to further, you have to open a file and create a file handle. Changing the assignment of the attempted file handle to a statement to evaluate them is beyond the scope of my response.
You can't simply put a filename in a statement like that and expect it to work.
Again, this is why I coded my response this way; I wanted to give you a working example, but leave it open so you *had* to do some of your own homework.
Feel free to ask questions after you learn how to open a file for 'reading'.
-stevieb
| [reply] |
|
|
|
|
|
|
sorry for my lack of info and thanks so much for your help, i will try the code you provided and submit questions.
basically the output file needs to have the following features:
List in ascending alpha order a-z, no quotations,even columns, inserts area code, inserts exchange prefix the same on all number unless specifically marked otherwise, as can be seen below where there is an exchange prefix specified.
so in other words the numbers that only show the 4 digit line number, need to all have the same 587 exchange prefix and 333 area code, the one in the example that has the different exchange prefix, that one needs to have the same area code of 333 as the others... hope this makes more sense!
| [reply] |
|
|
It does make sense, and I'm usually in the same boat as other Monks... we prefer people do actual research and testing. The only reason I went all out is because I now code in Python, so the rare chance while at the office I get to visit PerlMonks I take the opportunity to do a bit of work with Perl :)
I'll leave it up to you how to sort and format (Google and perldoc are your friends). The numbers, if you checked my output, already does what you need. Again, your input was lacking, so depending on other situations, you'll have to edit the code to fit.
Cheers,
-stevieb
| [reply] |
|
|
|
|
|
|
|
Re: total noob trying to format a phone list
by AnomalousMonk (Archbishop) on Apr 24, 2015 at 17:54 UTC
|
My comments would be along the lines of Laurent_R's above.
... i have a phone list in a text file ...
What is the format of data in this file? Just a few lines, please, not 10,000. Also, please use <c> ... </c> or <code> ... </code> tags around code, data and input/output. Please see Markup in the Monastery and Writeup Formatting Tips.
... i am wanting a script to sort them like this ...
Also, how do you want your output sorted? By telephone number, ascending (this is suggested by your output example in the OP)? Otherwise? (Update: I just took another look at the OP and must admit I can't see what the sort order even might be.)
Also, as ww has pointed out above, doing some research and writing some code, even incomplete, will show a desire to learn and will encourage monkish support.
Give a man a fish: <%-(-(-(-<
| [reply] [d/l] [select] |
|
|
thanks for the question, the output file needs to have the following features:
List in ascending alpha order a-z, no quotations,even columns, inserts area code, inserts exchange prefix the same on all number unless specifically marked otherwise, as can be seen below where there is an exchange prefix specified.
"Pope, David 7274"
"Christy, Kathy 7208"
"Payne, Dennis 7035"
"Moore, Sue 7101"
"Smith, Rick 8821"
"Wise, Phil 8823"
"Brown, Andrew 7255"
"Barnes, Pat 762-9023"
| [reply] [d/l] |
Re: total noob trying to format a phone list
by ww (Archbishop) on Apr 24, 2015 at 17:36 UTC
|
- Read Learning Perl (O'Reilly).
- Select from Where and how to start learning Perl for alternate explanations of anything that doesn't 'click' for you
- Apply what you've learned to understanding the code your "found."
- Apply what you've learned about open (both for 'read' and 'write'), @array, regex, ....
- Ask a new question when you're stumped about some facet of the program that -- by then, and it should not be all that long -- you'll be developing.
You'll find the Monks extraordinarily generous with their help... when you show that you've made some effort yourself (which is best done by posting your code, the errors and warnings you get (or an explanation of how your intent and the result vary).
One of the Monastery's proudest roles in helping newcomers (and not-so-new-comers) to learn.
| [reply] [d/l] [select] |
Re: total noob trying to format a phone list
by Laurent_R (Canon) on Apr 24, 2015 at 17:36 UTC
|
| [reply] |
|
|
hi,
all the numbers need to be formatted as (333)-321 unless they have a different exchange prefix in which case that needs to go in place of where the 321 would go
| [reply] |
Re: trying to format a phone list
by aaron_baugher (Curate) on Apr 28, 2015 at 02:30 UTC
|
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.
| [reply] [d/l] [select] |