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

Hi Monks...

I'm trying to write this if..elseif...condition statement and I'm receiving an error message.

Can someone take a peek at the code and tell me what's wrong? I wrote the statement as it appears in most books so I'm not sure what I did wrong.

if ($total_price < 20.00) { $ship_price = 3.50; } elseif (($total_price >= 20.00) && ($total_price < 50.00)) { $ship_price = 5.50; } elseif (($total_price >= 50.00) && ($total_price < 80.00)) { $ship_price = 10.50; }

Thanks.

Replies are listed 'Best First'.
Re: if..elseif...syntax problem
by Bilbo (Pilgrim) on Feb 27, 2003 at 15:55 UTC

    It should be elsif, not elseif.

    In fact, when I try running this I get an error telling me this very clearly:

    elseif should be elsif at ./test.pl line 18.
    syntax error at ./test.pl line 20, near ")
                   {"
    Execution of ./test.pl aborted due to compilation errors.
    
Re: if..elseif...syntax problem
by pfaut (Priest) on Feb 27, 2003 at 16:31 UTC

    There's no need to check that the price is greater than the previous limit since that's already been determined by the previous condition. Also, the '.00' isn't required although it shouldn't hurt and does make it pretty clear that you're dealing with monetary amounts.

    if ($total_price < 20.00) { $ship_price = 3.50; } elsif ($total_price < 50.00) { $ship_price = 5.50; } elsif ($total_price < 80.00) { $ship_price = 10.50; } # else free shipping?
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: if..elseif...syntax problem
by tachyon (Chancellor) on Feb 27, 2003 at 15:59 UTC

    The syntax is elsif not elseif. You need an else clause otherwise if the cost is >80 you get free (undef probably) shipping.....

    for ( my $total_price = 10; $total_price < 100; $total_price += 10 ) { if ($total_price < 20.00) { $ship_price = 3.50; } elsif (($total_price >= 20.00) && ($total_price < 50.00)) { $ship_price = 5.50; } elsif (($total_price >= 50.00) && ($total_price < 80.00)) { $ship_price = 10.50; } else { warn "that's expensive!\n"; $ship_price = 15; } my $grande_costa = $total_price + $ship_price; print <<STUFF; Total price: $total_price Shipping: $ship_price Grand Total: $grande_costa STUFF }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: if..elseif...syntax problem
by BrowserUk (Patriarch) on Feb 27, 2003 at 16:01 UTC

    No disprespect intended, but skipping the spelling of elsif -v- elseif issue, if you bought books which show indentation done that way, I suggest you take them back and see if you can get your money back.:^)


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: if..elseif...syntax problem
by Mr. Muskrat (Canon) on Feb 27, 2003 at 15:55 UTC
    It is spelled elsif.
      Thanks. I knew it was something simple and stupid on my part.
A reply falls below the community's threshold of quality. You may see it by logging in.