Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Elsif Clause Problem

by b310 (Scribe)
on May 10, 2004 at 15:19 UTC ( [id://352104]=perlquestion: print w/replies, xml ) Need Help??

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

Good Morning,

I'm writing a control structure elsif clause to test various arguments and display the result that meets the argument.

My clause relates to calculating the maximum total housing payment people can afford based on the income and debt information that was provided.

The key is the down payment percentage that is going to be placed for the purchase. The options on the form are: 5%, 10%, 15%, and 20%.

Anyone with a 5%, 10%, or 15% down payment, the ratio used is 28/36. Anyone with a 20% down payment, the ratio is 33/38.

To determine the maximum total housing payment affordable, the $totalpmt variable will represent the lower of the two calculations. For example: for a 20% down payment, you will use the 33/38. If the result of 33 is lower than 38; the payment should be the calculation of 33. If the result 33 greater than 38, the payment should be the calculation of 38.

At this time, my clause works correctly for anyone who puts either a 10%, 15% and 20% down payment. If someone wants to put 5% down, the result is incorrect.

At all times, the results for 5%, 10%, and 15% should be the same since they are using the same ratio of 28/36.

Please feel free to go to my form and test it yourself. The URL is:
http://www.summitwebcrafters.com/prequalification form.htm

Here's the input data:
income: 10000
auto loans: 1250
interest: 8
term: 30
Test each of the down payment percentages in the drop down box.

The result will appear in red.

The results are:
20% down = 2550
15% down = 2350
10% down = 2350
5% down = 2800 --- this is incorrect, it should be 2350 as well.

Below, is the code that I wrote to determine all this. I probably gave more information than necessary but more is better than less.

Any help you can give me in troubleshooting why 5% is not coming up with the right result is greatly appreciated.

Here's the code:
## Calculates the maximum monthly total housing payment (income / debt +) ## my $ratioincome28 = 0; my $ratioincome33 = 0; my $ratiodebt36 = 0; my $ratiodebt38 = 0; $ratioincome28 = $totalincome * .28; $ratioincome33 = $totalincome * .33; $ratiodebt36 = ($totalincome * .36) - $totaldebt; $ratiodebt38 = ($totalincome * .38) - $totaldebt; ## Determines the lower of the two ratios ## my $totalpmt = 0; if ( ($ratioincome33 gt $ratiodebt38) && ($downpercent eq 20) ) { $totalpmt = $ratiodebt38; } elsif ( ($ratioincome33 lt $ratiodebt38) && ($downpercent eq 20) ) { $totalpmt = $ratioincome33; } elsif ( ($ratioincome28 gt $ratiodebt36) && ($downpercent lt 20) ) { $totalpmt = $ratiodebt36; } else { $totalpmt = $ratioincome28; }
Thanks for all the help.

Replies are listed 'Best First'.
Re: Elsif Clause Problem
by neniro (Priest) on May 10, 2004 at 15:30 UTC
    A first good step: use numerical comparison operators instead of those for text comparision.
      Hi,

      Thank you for responding and giving me a hint as to where to start.

      I took your first suggestion. I changed all the operators to their respective numerical operators.

      Now, regardless of which down payment percentage I choose, the numerical result that I'm getting is the same for all of them which is 2550.

      Can the problem be related to the fact that I'm using a drop down box to select the down payment percentage in my form?

      Thanks.
        OT, but I'd use a datastructure to represent your equation constants in a table, and then have an algorithm calculate things based on that table and the input parameters.

        The if-comb is fine for simple things, but as the problem grows in complexity you're going to need to keep adding more if's. Treating the model as data instead of code will make things easier in the long run, but takes a little more work initially.

        I'll leave building that datastructure up to you, but in my world an if-comb typically (but not always) represents a design that could be improved to be more algorithmic. A particularly noteable point is a variable such as '$ratioincome28', which probably should not exist as a named variable in your program. Rather, that value might exist as an array value at index 28 or (better?) a hash key/value pair.

        In all, it's about maintainability and good coding practices for long term coding.

Re: Elsif Clause Problem
by pbeckingham (Parson) on May 10, 2004 at 16:19 UTC

    I suggest structuring your if/elsif/else structure differently, to reflect that there are nested conditions, and this will make the code a little more readable. Using numerical operators will help. Beware of use of > when >= may be needed.

    if ($downpercent == 20) { $totalpmt = $retioincome33 > $retiodebt38 ? $ratiodebt38 : $ratioinc +ome33; } else { $totalpmt = $ratioincom28 > $ratiodebt36 ? $ratiodebt36 : $ratioinco +me28; }

      Hello:

      Thank you very much for your help. The nested conditions did the trick. My results are appearing correctly.
      Thanks again.
Re: Elsif Clause Problem
by talexb (Chancellor) on May 10, 2004 at 17:23 UTC

    What does the Perl debugger tell you?

    perl -d yourScript.pl

    Alex / talexb / Toronto

    Life is short: get busy!

Re: Elsif Clause Problem
by pbeckingham (Parson) on May 10, 2004 at 16:03 UTC

    If this is homework, and the problem to be solved is only the problem stated, then my comment should be ignored, and I apologize for preaching.

    If, however, this calculator is to be used in the real world, you have made incorrect assumptions: a customer may place a deposit of any amount, not just the discrete 5%, 10%, 15% and 20% examples. Further, 'auto loan' ignores student loans, other mortgages, levies, revolving debt, alimony, child support and many other forms of debt, so the field should be renamed at a minimum. Setting interest as a single value ignores the many varied instruments that make up the spectrum of loan types.

Re: Elsif Clause Problem
by zude (Scribe) on May 10, 2004 at 16:09 UTC
    You're using string compare operators.
    "5" lt "20" is false.
    5 < 20 is true.

    I am slow.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-23 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found