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;
while ( $i < $#ARGV)
{ # process args
if ( @ARGV[$i] eq "-r" )
{
$RATE=@ARGV[++$i]; # interest rate
}
else
{
if ( @ARGV[$i] eq "-a" )
{
$AMOUNT=@ARGV[++$i]; # loan amount
}
else
{
if ( @ARGV[$i] eq "-p" )
{
$n = 0;
while ( $i < $#ARGV)
{
@PAYMENT[$n++]=@ARGV[++$i]; # payment amount
}
}
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
}
if ($AMOUNT == 0 || $RATE == 0 || @PAYMENT[0] == 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; # get actual monthly percentage rate
$balance = $AMOUNT;
@payment = @PAYMENT;
$n--;
$x = 0;
while ($balance > 0)
{ # round up interest amount
$interest = roundUpAmount($rate * $balance);
$principal = roundUpAmount(@payment[x] - $interest);
if ( $balance < $principal )
{ # last payment
$principal = $balance; # don't pay too much!
@payment[x] = $principal + $interest;
}
$balance = roundUpAmount($balance - $principal);
print "$month\t\$@payment[x]\t\$$interest\t\t\$$principal\t\t\$$balance\n";
$month++;
if ($x != $n)
{
$x++;
}
}
print $x;
print $n;
sub roundUpAmount {
#
# in: floating point monetary value
# out: value rounded (and truncated) to the nearest cent
#
$value=$_[0];
$newvalue = ( int ( ( $value * 100 ) +.5 ) ) / 100;
return ($newvalue);
}
kinda wish i knew why mine isnt working? anyone see something wrong with it? im still a little new to perl, might be a syntax thing.