LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Script Help (https://www.linuxquestions.org/questions/programming-9/perl-script-help-237987/)

Rv5 10-02-2004 03:22 PM

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

CroMagnon 10-02-2004 05:13 PM

This seems to work. Hope this wasn't a homework assignment!

Code:

#!/usr/bin/perl
$i=0;
$p=0;
@PAYMENT = ();
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[$p++]=@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";
foreach (@PAYMENT) {
    print "Monthly payment: \$$_\n";
}
print "\n";
print "Month\tPayment\tInterest\tPrincipal\tBalance\n\n";

$month=1;
$rate=$RATE/12/100;
$balance=$AMOUNT;
$payment=@PAYMENT[0];

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";
    $payment=@PAYMENT[ $month < @PAYMENT-1 ? $month: @PAYMENT-1];
    $month++;
}

sub roundUpAmount {
    $value=$_[0];
    $newvalue = ( int ( ( $value * 100 ) +.5 ) ) / 100;
    return ($newvalue);
}


Rv5 10-02-2004 05:38 PM

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.

CroMagnon 10-02-2004 05:58 PM

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].

Rv5 10-02-2004 06:07 PM

damnit, of course! thanks for the help CM!

chrism01 10-03-2004 10:04 AM

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

sub get_cmd_line_params
{
    # Get the cmd line options
    getopts('c:');

    # Check if customer id supplied
    if( ! $Getopt::Std::opt_c )
    {
        # Not supplied, so warn & exit
        usage();
    }

    # Return customer id
    return( $Getopt::Std::opt_c );
}

See http://iis1.cps.unizar.es/Oreilly/pe...ok/ch15_02.htm


All times are GMT -5. The time now is 09:03 AM.