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

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)

Replies are listed 'Best First'.
Re^5: total noob trying to format a phone list
by hippo (Archbishop) on Apr 27, 2015 at 16:28 UTC

    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.