in reply to trying to format a list

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

Replies are listed 'Best First'.
format a list
by sonikd (Initiate) on Apr 24, 2015 at 20:21 UTC
    Hi, i think i am a little further along than i was now, but i am getting a message:

    Global symbol "$num" requires explicit package name at <path>

    heres the code as i have it:

    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"; }

      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:
      1. code
      2. verbatim error and/or warning messages
      3. a coherent explanation of what "doesn't work actually means.

      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

        Hello guys, thanks so much for sticking with me on this, OK, i have read a few pages in the Beginning Perl chap6 on file handlers, still a little fuzzy to me but i am trying.

        heres the code as i have it: (also the errors im getting below that)

        use warnings; use strict; open my $fh, '+>', 'output.txt' or die("Can't open the damn file!: $!"); open FILE, 'c:/aperlphone/myphonelist.txt' or die $!; while(my $line == 'FILE') # semi-colon removed(run1) & (run2)equit +y 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"; }
        Use of uninitialized value $line in numeric eq (==) at C:\aperlphone\P +honeListFinalX.pl line 9. Use of uninitialized value $line in scalar chomp at C:\aperlphone\Phon +eListFinalX.pl line 11. Use of uninitialized value $line in split at C:\aperlphone\PhoneListFi +nalX.pl line 12. Use of uninitialized value $num in substitution (s///) at C:\aperlphon +e\PhoneListFinalX.pl line 16. Use of uninitialized value $num in pattern match (m//) at C:\aperlphon +e\PhoneListFinalX.pl line 18. Use of uninitialized value $num in pattern match (m//) at C:\aperlphon +e\PhoneListFinalX.pl line 21. Use of uninitialized value $name in concatenation (.) or string at C:\ +aperlphone\PhoneListFinalX.pl line 25. Use of uninitialized value $num in concatenation (.) or string at C:\a +perlphone\PhoneListFinalX.pl line 25. Use of uninitialized value $line in numeric eq (==) at C:\aperlphone\P +honeListFinalX.pl line 9. Use of uninitialized value $line in scalar chomp at C:\aperlphone\Phon +eListFinalX.pl line 11. Use of uninitialized value $line in split at C:\aperlphone\PhoneListFi +nalX.pl line 12. Use of uninitialized value $num in substitution (s///) at C:\aperlphon +e\PhoneListFinalX.pl line 16. Use of uninitialized value $num in pattern match (m//) at C:\aperlphon +e\PhoneListFinalX.pl line 18. Use of uninitialized value $num in pattern match (m//) at C:\aperlphon +e\PhoneListFinalX.pl line 21. Use of uninitialized value $name in concatenation (.) or string at C:\ +aperlphone\PhoneListFinalX.pl line 25. Use of uninitialized value $num in concatenation (.) or string at C:\a +perlphone\PhoneListFinalX.pl line 25. Use of uninitialized value $line in numeric eq (==) at C:\aperlphone\P +honeListFinalX.pl line 9. Use of uninitialized value $line in scalar chomp at C:\aperlphone\Phon +eListFinalX.pl line 11. Use of uninitialized value $line in split at C:\aperlphone\PhoneListFi +nalX.pl line 12. Use of uninitialized value $line in split at C:\aperlphone\PhoneListFi +nalX.pl line 12. Terminating on signal SIGINT(2)
trying to format list
by sonikd (Initiate) on Apr 24, 2015 at 18:17 UTC

    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!

      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

        I now code in Python
        I really feel sorry for you. ;-)

        Je suis Charlie.