in reply to Re: total noob trying to format a phone list
in thread trying to format a list

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

Replies are listed 'Best First'.
Re^3: total noob trying to format a phone list
by ww (Archbishop) on Apr 24, 2015 at 22:07 UTC

    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.
Re^3: total noob trying to format a phone list
by stevieb (Canon) on Apr 25, 2015 at 00:42 UTC

    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)

        At no point in your code do you assign any value to $line so it is no great surprise that you see all those warnings to say that $line and the other variables which are extracted from it are uninitialized.

        This line is likely to be the cause of (some of) your troubles:

        while(my $line == 'FILE')

        Here you are numerically comparing a variable with no value ($line) with a string ('FILE'). There is every chance that what you would actually want to do here is assign a value to $line on the basis of a line read from your input file handle. eg:

        while (my $line = <FILE>)

        Note the difference between the numeric comparison operator == and the assignment operator =. See perlop for the details. HTH.