Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Modeling FIFO financial transactions in Perl

Expand Messages
  • Peter L. Berghold
    ... It is possible that I don t fully understand the problem, but I would think that a FIFO would be trivial to implement in Perl. Consider the following: my
    Message 1 of 3 , Mar 10, 2010
    • 0 Attachment
      On Wed, 2010-03-10 at 10:36 -0700, Kelly Jones wrote:
      > How do I easily model first-in-first-out (FIFO) financial transactions
      > in Perl?

      It is possible that I don't fully understand the problem, but I would
      think that a FIFO would be trivial to implement in Perl.

      Consider the following:

      my @transactions=();

      unshift @transactions,{ stock=>'stock a', price => 1.99 };
      unshift @transactions,{ stock=>'stock b', price => 3.99 };

      my $first = pop @transactions;
      my $second = pop @transactions;

      Unless I'm missing something here that should emulate the behavior of a
      FIFO.

      Another method to consider which is more complex is to time stamp the
      individual transactions and while storing them in an array you sort on
      the time stamp.

      push @transactions, { stock=>'stock a', timestamp=> time , price =>
      100 };

      ------- wait a while ---------

      push @transactions, { stock=> 'stock a', timestamp => time , price =>
      101 };

      @transactions = sort { $a->{timestamp} <=> $b->{timestamp} },
      @transactions;

      my $first = shift @transactions;

      Doing that would populate $first with the earliest time stamped
      transaction.



      --
      Peter L. Berghold, Australian Cattle Dog Owner, Agility Fan, Foodie,
      Salty Old Dog and Old School Unix Hacker.
      Skype: cowdawg
      "Those who fail to learn from history are condemned to repeat it"
    • merlyn@stonehenge.com
      ... Peter @transactions = sort { $a- {timestamp} $b- {timestamp} }, Peter @transactions; That comma doesn t go there. It s either: sort EXPRESSION, LIST
      Message 2 of 3 , Mar 10, 2010
      • 0 Attachment
        >>>>> "Peter" == Peter L Berghold <peter@...> writes:

        Peter> @transactions = sort { $a->{timestamp} <=> $b->{timestamp} },
        Peter> @transactions;

        That comma doesn't go there.

        It's either:

        sort EXPRESSION, LIST

        or

        sort { STATEMENTS } LIST

        Note the absence of comma in the latter.
        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
      Your message has been successfully submitted and would be delivered to recipients shortly.