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

Greetings fellow monks,

Thanks so much for the help you've already given me.
Now I'm learning how to use the elsif conditional.
My script is producing the following error:

Your script produced this error: syntax error
at ./elsif.cgi line 7, near ") &&"

Thanks for taking the time to read this post.
I look forward to reading your response.
require TripodCGI; $CGI=new TripodCGI; $firstName=$CGI->param('firstName'); $Age=$CGI->param('Age'); print "Content-type: text/html\n\n"; print "<HTML><TITLE>Conditionals test</TITLE> </HEAD><BODY>"; if ($Age < 13) && ($Age > 0)) { print "What are you doing here, $firstName? This Web site is strictly PG-13. Shame on you for being so naughty as to come here!"; } elsif ($Age < 18) && ($Age > 12)) { print "Not to long ago, we’d have have chased you away from our Web site. But now that you’re a teenager and mature enough for PG-13 materials, we’re glad to see you $firstName."; } elsif ($Age < 30) && ($Age > 17)) { print "If you’re viewing this Web site on your work computer, you should be aware that the materials presented on this Web site are, in their own way, as inflammatory as pornography. You could get in a lot of trouble, $firstName."; } elsif ($Age < 40) && ($Age > 29)) { print "Congratulations, $firstName, you’re on longer a callow youth. Never trust anyone under 30; that’s what we say!"; } elsif ($Age < 60) && ($Age > 39)) { print "You’re getting on in years, $firstName. It’s time to start thinking about establishing retirement savings."; } else { print "So you’re still dirt poor, $firstName? Oh well, guess you always will be. Those are the breaks, oldster."; } print "</BODY></HTML>";

Replies are listed 'Best First'.
Re: A newbie's first try at conditionals
by DamnDirtyApe (Curate) on Aug 17, 2002 at 01:53 UTC

    Your parentheses aren't balanced. You want

    if (($Age < 13) && ($Age > 0)) | | +-- Add this one.

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: A newbie's first try at conditionals
by Zaxo (Archbishop) on Aug 17, 2002 at 01:58 UTC

    Looks fine, excepr for what the error message says, a syntax error, you're missing a left paren in each condirional:

    elsif (($Age < 60) && ($Age > 39))
    for example. You ought to improve your indentation style ( see perlstyle).

    After Compline,
    Zaxo

Re: A newbie's first try at conditionals
by Maclir (Curate) on Aug 17, 2002 at 03:30 UTC
    As others have mentioned, your parentheses are unbalanced. I have a similar problem in complex conditional statements, and I have found a trick that helps me - at least when I am developing the code.

    Instead of having the conditional all on the one line, like if (($Age < 13) && ($Age > 0)) I try to space things out, like:

    if (($Age < 13) && ($Age > 0) ) { some block of statements; } elsif (($Age < 18) && ($Age > 12)) ) { another block of statements; } else { a final block; }
    This makes a visual scan of the whole if - elsif - else block easier. Once it all works, you cansquash the lines up if you wish.

    Once you start to get into complex data structures then this can solve a lot of stilly typographical erors.

Re: A newbie's first try at conditionals
by jlongino (Parson) on Aug 17, 2002 at 17:11 UTC
    Your conditional doesn't seem to do what you want. What happens if $Age = 0? It takes the final else clause you want to use for oldsters. The example below corrects this problem and IMHO, is easier to read.
    if ($Age > 0) { if ($Age < 13) { # ... } elsif ($Age < 18) { # ... } elsif ($Age < 30) { # ... } elsif ($Age < 40) { # ... } elsif ($Age < 60) { # ... } else { # ... } }

    --Jim