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

In my feeble attempt at code below I am having trouble with this statement. The clients database of discounts is simple in most cases. usually the discounts are 40 or 55 flat numbers. However they have a few cases of "40+15" which means take off 40% and then from the resulting number take off 15%. Math says that means a discount of 49%. I am trying to convert all instances of 40+15 to a discount of 49%. That in itelf wouldn't be too hard except for another issue. If the discount isn't 40+15 the website needs to take off an additional 2% if there are 5 or more items purchased and and additional 3% (total) if there are 10 or more items... I wrote the code below to handle that, but for some reason the script is thinking that everything is == to "40+15". does anyone know a better way to do this?
 
$discount = $cart_fields[$sc_cart_index_of_product_discount]; if ($discount == "40+15") { $discount = 49; } else { if ($quantity >= 5) { $discount = ($discount+3); if ($quantity < 10) { $discount = ($discount-1); } } }

 
Sologretto

Replies are listed 'Best First'.
Re: an odd if then statement
by suaveant (Parson) on Jul 23, 2001 at 21:28 UTC
    use eq instead of ==...
    == is to compare numbers, but 40+15 is actually a string, so you need eq...

    Update 40+15 in quotes is a string, without quotes it would be the number 55, but you are matching it to the string '40+15' in your variable...

                    - Ant

      Thanks MUCH all :-)
Re: an odd if then statement
by HyperZonk (Friar) on Jul 23, 2001 at 21:33 UTC
    One of the big problems here is that == is used for number equality, not string equality. Use
    if ($discount eq '40+15')
    To do what you are attempting in this line.

    However, because things may change, this is probably not the best way to approach what you are trying to do. What if someday you need to be able to evaluate '35+15' or some other such thing? If the discounts are always going to appear in this format, you could do something like:
    @discount = split(/\+/, $discount); $discount = 1; foreach (@discount) { $discount*=$_/100; } $discount*=100;

    and then do your quantity calculations.

    -HZ
Re: an odd if then statement
by Sifmole (Chaplain) on Jul 23, 2001 at 21:28 UTC
    Well "40+15" looks alot like a string to me.... and you are using "==" which is for comparing numbers. You might want to check out the "eq" statement.
    if ("word1" == "word2") { print "this is always true"; } else { print "We will never get here."; }
Re: an odd if then statement
by Hofmator (Curate) on Jul 23, 2001 at 21:31 UTC

    == != eq ;)

    This means you are doing a numeric compare but you want an string compare here: if ($discount == "40+15") as 40+15 is only partly numeric. If you are sure that there are no whitespaces this is the way to go, otherwise (to allow sth like "40 + 15") use a regex if ($discount =~ /^\s*40\s*+\s*15\s*$/)

    -- Hofmator