LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2004, 03:22 PM   #1
Rv5
Member
 
Registered: Sep 2003
Distribution: RedHat9
Posts: 114

Rep: Reputation: 15
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
 
Old 10-02-2004, 05:13 PM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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);
}
 
Old 10-02-2004, 05:38 PM   #3
Rv5
Member
 
Registered: Sep 2003
Distribution: RedHat9
Posts: 114

Original Poster
Rep: Reputation: 15
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.
 
Old 10-02-2004, 05:58 PM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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].
 
Old 10-02-2004, 06:07 PM   #5
Rv5
Member
 
Registered: Sep 2003
Distribution: RedHat9
Posts: 114

Original Poster
Rep: Reputation: 15
damnit, of course! thanks for the help CM!
 
Old 10-03-2004, 10:04 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
please help .perl Script/ apenguinlinux Programming 4 08-10-2005 09:04 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM
perl script... killjoy Programming 0 03-29-2001 03:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration