The Elite Noob has asked for the wisdom of the Perl Monks concerning the following question:

So, i wrote this simple script to check for prime numbers. Anyways i keep getting a syntax error, but can't figure out what it is. So this is the code:
#!/usr/bin/perl -w # vanillaPrimes.pl use strict; my $divider; # Used for testing file. open FILE, "<", "DATA1.txt" or die $!; # Open a file or return error. open oFILE, "+>>", "OUT1.txt" or die $!; # Used to write answer. my $numInput = <FILE>; # Getting the line from file. chomp($numInput); # removing a newline, just in case. my $half; if($numInput % $divider == 0){ $half = $numInput/2 } else { $half = ($numInput - 1)/2 } foreach($divider..$half){ if($numInput % $divider == 0){ print oFILE "not"; } else { print oFILE "prime"; } } close(FILE); close(oFILE);
And this is the error i have.
syntax error at vanillaPrimes.pl line 13, near "){"
Execution of vanillaPrimes.pl aborted due to compilation errors.
This is line 13.
foreach($divider..$half){
Thanks for the help, and please don't make a new script :) just help me with the error. Thanks!

Replies are listed 'Best First'.
Re: Simple Syntax Error? Can't Find?
by GrandFather (Saint) on Feb 26, 2011 at 00:03 UTC

    There is no syntax error in the code you post, but there is a major runtime error: $divider is not initialised anywhere.

    You will discover another runtime problem later on, but it ought make itself pretty obvious and you will probably figure it out quickly.

    True laziness is hard work
Re: Simple Syntax Error? Can't Find?
by ikegami (Patriarch) on Feb 26, 2011 at 00:39 UTC
    That error usually indicates a missing semi-colon after the previous statement.
    >perl -e"my $x = 1 if (foo()) { bar() }" syntax error at -e line 1, near ") {" Execution of -e aborted due to compilation errors. >perl -e"my $x = 1 for (foo()) { bar() }" syntax error at -e line 1, near ") {" Execution of -e aborted due to compilation errors.
Re: Simple Syntax Error? Can't Find?
by BrowserUk (Patriarch) on Feb 26, 2011 at 00:04 UTC

    Strange. I pasted you code into a file and it syntax checks clean:

    C:\test>type junk.pl #!/usr/bin/perl -w # vanillaPrimes.pl use strict; my $divider; # Used for testing file. open FILE, "<", "DATA1.txt" or die $!; # Open a file or return error. open oFILE, "+>>", "OUT1.txt" or die $!; # Used to write answer. my $numInput = <FILE>; # Getting the line from file. chomp($numInput); # removing a newline, just in case. my $half; if($numInput % $divider == 0){ $half = $numInput/2 } else { $half = ($numInput - 1)/2 } foreach($divider..$half){ if($numInput % $divider == 0){ print oFILE "not"; } else { print oFILE "prime"; } } close(FILE); close(oFILE); C:\test>perl -c junk.pl junk.pl syntax OK

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Simple Syntax Error? Can't Find?
by TomDLux (Vicar) on Feb 26, 2011 at 01:19 UTC

    I don't get any error when I try to run your file. But I also disagree with your idea of line 13. I say line 13 is

    if($numInput % $divider == 0){

    That line has an error because $divider is uninitialized when you try to use it as the divisor in a mudulus operation. I do think you should use warnings; rather than have -w in line one ... the -w is a subset and I suspect would give you more information about this situation..

    It seems like you actually intend to determine whether the input number is odd or even. No need. You can collapse that five lines of code into a simple expression:

    my $half = int( $numInput / 2);

    If $numInput is even, $numInput / 2 is an integer, so int() of it has no effect. if $numInput is odd, dividing by 2 leaves something-and-a-half, , so int() of that is the int without the fraction.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      Thanks to everyone! i solved, it The problem was the semi colons. LOL thanks :) can't believe i missed it...
Re: Simple Syntax Error? Can't Find?
by Anonymous Monk on Feb 26, 2011 at 00:18 UTC

    Well, I haven't figured out the source of your syntax error (on Windows ActivePerl, it's not representing, and putty won't paste correctly into emacs from this machine), but your script dies with an illegal modulus zero 100% of the time, because you never initialize $divider before you use it. I'm not sure if this is the problem you're seeing, but I'm fairly sure that foreach(undef .. whoKnows) is a bad idea, and that's what your line 13 resolves to.

    Also, the omitted semi-colons on lines 14 and 16 are probably bad ideas.