• 0

[Perl] syntax errors? initializing variables


Question

namesEnhanced.pl

   #!/usr/bin/perl -w

 #%names = ();
 $fn = "";
 $ln = "";

 while (<>){

		 chomp;

		 ($fn, $ln) = split;
		 $names{$ln} = $fn;

 }

 foreach $ln (sort keys %names){

		 print "$ln, $names{$ln}\n"; #prints last name (key), first name (value)

 }

names.txt (the file that I call on the command line to read from)

[text]

Barack Obama

Chad Johnson

Brady Quinn

George Bush

[/text]

When I run % ./namesEnhanced.pl names.txt I get:

line 4: syntax error near unexpected token `('

line 4: `%names = ();'

So.... that's strange. I comment that out and run:

line 5: =: command not found

line 6: =: command not found

line 8: syntax error near unexpected token `)'

line 8: `while (<>){'

...

I am simply modifying a perl script that I made before that takes input from the keyboard instead of a file. The other script runs just fine... What is going on?!

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

weird. I just copied and pasted that script, even uncommented out your %names =(); and it worked fine for me. Passed ina text file with first and last names then it displayed them on the screen for me with your desired format.

Link to comment
Share on other sites

  • 0

Ooook I just got it to work. I just remembered that I had accidentally tried to compile it with gcc, and I bet that it somehow messed it up because I just copied and pasted it into a new file and it works.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.