Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

syntax error near else and }

by maitai091054 (Initiate)
on Feb 04, 2012 at 23:16 UTC ( #951874=perlquestion: print w/replies, xml ) Need Help??

maitai091054 has asked for the wisdom of the Perl Monks concerning the following question:

I am new to Perl and taking a class. I am getting the following errors: syntax error at ./B16a line 44, near "else", syntax error at ./B16a line 54, near "}"

I have matched up all {} and they seem fine and to me the else looks fine as well. Any assistance would be appreciated.

1 #!/usr/bin/perl 2 3 use warnings; 4 5 print "Enter numbers from 1 to 20 and/or letters seperated by a sp +ace:", "\n "; 6 print "Enter q or Q to quit", "\n"; 7 ($list = <STDIN>); 8 chomp $list; 9 print $list, "\n"; 10 print "length of list: " , length($list), "\n"; 11 $howlong = length($list); 12 print "howlong = ", $howlong, "\n"; 13 $sum = 0; 14 $pointer = 0; 15 $hold = ""; 16 $work = ""; 17 while (!$flag and $pointer <= $howlong +1) 18 { 19 $work = substr($list,$pointer,1); 20 if ($work eq "q" or $work eq "Q") { 21 $flag = true; 22 print "flag = , $flag, ", "\n"; 23 } 24 if ($work ne "q" and $work ne "Q") { 25 if ( $work =~ /^[\+-]*[0-9]*\.*[0-9]*$/ && $work !~ /^[\. ]*$/ + ) { 26 if ($work == ' ') { 27 if ($pointer > 0) { 28 $hold = "$hold"." + "."$work"; 29 print "pointer = ", $pointer, "\n"; 30 print "hold = ", $hold, "\n"; 31 print "work = ", $work, "\n"; 32 $sum = $sum + $work; 33 $pointer++; 34 } 35 else { 36 $hold = "$hold"."$work"; 37 $sum = ($sum + $work); 38 $pointer++; 39 } 40 } 41 else { 42 $hold = $hold.$hold; 43 } 44 else { 45 $pointer++; 46 print "has alpha: ", $work, "\n"; 47 } 48 } 49 else { 50 print ($hold, " = ", $sum, "\n"); 51 } 52 53 } 54 }

Replies are listed 'Best First'.
Re: syntax error near else and }
by InfiniteSilence (Curate) on Feb 04, 2012 at 23:57 UTC

    Problems:

    • You are using warnings but not strict. Add use strict to your code and watch what happens.
    • Lots and lots of nested if statements and the subsequent need to indent is normally a sign that you should strongly consider using subroutines and even a switch statement in your code.
    • I hate it when people post code with line numbers.
    • "Q" or "q" really won't make the input from STDIN quit...the CR does -- try:  perl -e 'print qq|Enter q or Q to quit|; ($list = <STDIN>);'
    • On the same subject, you could have done:   perl -e 'print qq|Give me 20 #s now dammit! |; ($list = <STDIN>); my @stuff = split q| |, $list; print scalar @stuff;' where you could have died if there were < 20 numbers entered: die 'not enough numbers!' unless (@stuff == 20);
    • This isn't very readable and you should be using eq instead: ...$work == ' '). How many spaces is that -- 2 or 3? Since I cannot tell immediately I don't know. Try,  perl -e 'my $work = q~     ~; if ($work eq q| | x 5){print 1};' or perhaps a regex.
    I recommend that you check to see if your code is on par with the other students in your class. If not then you might try and reread whatever material you were given. If it is look ahead in your materials to see if, near the end of the course, you will be expected to write terse, readable, working code. If not, consider leaving the class and getting your money back. Your time and money would be far better invested in a good book. There are recommendations available at Perl.com.

    Celebrate Intellectual Diversity

Re: syntax error near else and }
by choroba (Archbishop) on Feb 04, 2012 at 23:18 UTC
    else is used after if, but you are using it after else which is not correct.

      Can I suggest you take a look at this link to Given/When example.

        Do you really mean me by "you"?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://951874]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-05-24 00:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?