Perl Script Help
ive written the following script:
---------------------------------- $i=0; while ( $i < $#ARGV) { if ( @ARGV[$i] eq "-r" ) { $RATE=@ARGV[++$i]; } else { if ( @ARGV[$i] eq "-a" ) { $AMOUNT=@ARGV[++$i]; } else { if ( @ARGV[$i] eq "-p" ) { $PAYMENT=@ARGV[++$i]; } else { print "Unknown argument (@ARGV[$i])\n"; exit } } } $i++; } if ($AMOUNT == 0 || $RATE == 0 || $PAYMENT == 0) { print "Specify -r rate -a amount -p payment\n"; exit } print "Original balance: \$$AMOUNT\n"; print "Interest rate: ${RATE}%\n"; print "Monthly payment: \$$PAYMENT\n"; print "\n"; print "Month\tPayment\tInterest\tPrincipal\tBalance\n\n"; $month=1; $rate=$RATE/12/100; $balance=$AMOUNT; $payment=$PAYMENT; while ($balance > 0) { $interest=roundUpAmount($rate * $balance); $principal=roundUpAmount($payment - $interest); if ( $balance < $principal ) { $principal=$balance; $payment=$principal + $interest; } $balance = roundUpAmount($balance - $principal); print "$month\t\$$payment\t\$$interest\t\t\$$principal\t\t\$$balance\n"; $month++; } sub roundUpAmount { $value=$_[0]; $newvalue = ( int ( ( $value * 100 ) +.5 ) ) / 100; return ($newvalue); } ------------------------------------------------ it only takes in one payment amount though. i was hoping someone could help me modify it so it can accept multiple different payment amounts, and just use the last payment amount to finish off the loan. for example say the loan is 100 bucks, the user should be able to enter in 20 and 10. it will pay off 20 the first month and use 10 to for the rest of the months. i had tried a couple of different things and it kept crashing. i was thinking that i could make the PAYMENT variable an array but Im having a hard time taking in multiple arguments from the command line. any ideas or suggestions? Thanks |
This seems to work. Hope this wasn't a homework assignment!
Code:
#!/usr/bin/perl |
thanks that does seem to be working. this is a modification of a script i wrote for a webpage at work. just to show a bit more effort, heres where i was at when i saw your post. not sure why mine isnt working? the indexes all seem to be incrementing right, but the array kept using the very first one?
Code:
$i=0; |
Looks like you've missed a $ off the array index on a couple of lines. That is, @payment[x] where you should have @payment[$x].
|
damnit, of course! thanks for the help CM!
|
2 things:
In Perl a single value is always a scalar, even if it's part of an array or hash ie Array @payment array element $payment hash %my_hash hash element $my_hash{$key} = $value Have you thought of using Getopts for the cmd line? eg Code:
use Getopt::Std; # Get cmd line params |
All times are GMT -5. The time now is 09:03 AM. |