in reply to format a list
in thread trying to format a list

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

  • Comment on Re^3: total noob trying to format a phone list

Replies are listed 'Best First'.
format a list
by sonikd (Initiate) on Apr 27, 2015 at 14:18 UTC

    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.