Sunday, 13 June 2010

Personal Finance in Perl: part 2

As mentioned last week, I've decided to exercise my mad leet perl skillz by writing my own personal finance software, and in the absence of feeling free to blog about anything else, I thought I'd share it with you.

It works by carrying out various neat filtery things on a a ledger which you enter all you financial transactions into. This is the bit of code for entering transactions into the ledger.

sub enteringTransactions {
$transaction_date = "01/01/2000";
$transaction_account = "dummy";
$transaction_value = "9999";
$transaction_payee = "dummy";
$transaction_category = "dummy";
$notes = "dummy dummy";
print " Entering transactions\n";
print "Please enter the date of the transaction dd/mm/yyyy:- ";
chomp($trans_date = );
@transaction_date = split /\//, $trans_date;
$trans_day = $transaction_date[0]*1;
$trans_month = $transaction_date[1]*1;
$trans_year = $transaction_date[2]*1;
$ninday = $trans_day+($trans_month*31)+($trans_year*12*31);
print "Please enter account name (cash, hsbc or credit):- ";
chomp($transaction_account = );
print "Please enter value (xx.xx):- ";
chomp($transaction_value = );
print "Please enter payee:- ";
chomp($transaction_payee = );
print "Please enter category:- ";
chomp($transaction_category = );
print "$trans_day/$trans_month/$trans_year\t$transaction_account\t$transaction_value\t$transaction_payee\t$transaction_category\n";
$entry_time = time;
my $filename = "chriscounts.poo";
open LOG, ">>", $filename or die "Can't open '$filename': $!";
print LOG "$entry_time, $trans_day, $trans_month, $trans_year, $ninday, $transaction_account, $transaction_value, $transaction_category, $transaction_payee, $notes\n";
close LOG;
print "Transaction logged\n"};

The ledger is a file called, rather amusingly, "chriscounts.poo" and the data is stored in as a comma space separated line with the following information:-
  • timestamp of when the data was entered, in milliseconds
  • day of month of transaction
  • month of transaction
  • year of transaction in four digits
  • data in my own proprietary format for the purpose of sorting and doing neat things with
  • transaction account
  • value of transaction
  • category
  • payee
  • notes, although this bit isn't used.
In an ideal world where I knew what I was doing, there would be some kind of encryption routine here as well so if evil miscreants got the ledger file, it would all look like gobbledigook. But that's going to have to come later.

Right, this next subroutine is what I use to load the ledger file into memory as an array of arrays
sub getLedger {
@ledger = ();
my $filename = "chriscounts.poo";
open LEDGER, "<", $filename or die "Can't open '$filename': $!";
while () {
@tmp = split/, /;
push @ledger,[ @tmp ];
close LEDGER;

No comments:

Post a Comment