Home  |  Linux  | Mysql  | PHP  | XML
From:Jay Savage Date:Wed Mar 10 18:46:01 2010
Subject:Re: Modeling FIFO financial transactions in Perl
On Wed, Mar 10, 2010 at 12:36 PM, Kelly Jones
<kelly.terry.jones@gmail.com> wrote:
> How do I easily model first-in-first-out (FIFO) financial transactions
> in Perl? Example:
>
>
>
> I can think of some ugly ways to model this in Perl, but no good/clean
> ways. Any thoughts?

The normal way to implement FIFO in Perl is by using push and shift on
arrays: push new data on the tail, shift old data off the head.

Addition is commutative, so you don't need to calculate the profit per
share to calculate the total profit. Just keep an array of the price
paid for each share. The following is long hand, but it illustrates
the basic principles of of FIFO in Perl.

use warnings;
use strict;

my @bought;

# days 1,2,3
my @days = ( 8, 9, 10 );

foreach my $price (@days) {
push( @bought, $price ) for ( 1 .. 100 );
}

# day 4
my $sell_price = 11;
my $sell_shares = 150;
my $gross = $sell_price * $sell_shares;
my $cost = 0;
for ( 1 .. $sell_shares ) {
$cost += shift @bought;
}

# step 3: profit!
print 'We made $', $gross - $cost, " today!!\n";


HTH,

-- j
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

Navigate in group perl.beginners at sever nntp.perl.org
Previous Next


Your recent visits
stripped output from Net::SSH2
Re: Any Good SCM tool to manage Perl Code locally
Re: Any Good SCM tool to manage Perl Code locally
Re: 1-line datafile, need data for another routine
Re: 1-line text file with numbers, need to add to them



  
© No Copyright
You are free to use Anything, but please consult your advocate before doing so as this website
also list content from other sources which may be copyrighted.
Site Maintained by Zareef Ahmed
Powered By PHP Consultants