From 865a4f39e254f3dca7227df366e91d6c6380f68d Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Mon, 19 Dec 2011 11:42:31 -0500 Subject: [PATCH] Parse negative numbers better. --- external-account-totals-reconcile.plx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/external-account-totals-reconcile.plx b/external-account-totals-reconcile.plx index 5e54173..0b35862 100755 --- a/external-account-totals-reconcile.plx +++ b/external-account-totals-reconcile.plx @@ -33,8 +33,12 @@ my $LEDGER_CMD = "/usr/bin/ledger"; my $ACCT_WIDTH = 75; sub ParseNumber($) { - $_[0] =~ s/,//g; - return Math::BigFloat->new($_[0]); + my($val) = @_; + $val =~ s/,//g; + $val =~ s/\s+//g; + $val = - $val if $val =~ s/^\s*\(//; + + return Math::BigFloat->new($val); } @@ -57,7 +61,7 @@ while (my $line = ) { $line =~ s/^\s*//; $line =~ s/\s*$//; next unless $line =~ - /^\s*(\S+\:.+)\s+[\(\d].+\s+\(?\s*([\d\.\,]+)\s*\)?\s*$/; + /^\s*(\S+\:.+)\s+[\(\d].+\s+([\(?\s*\d\.\,]+)\s*\)?\s*$/; my($acct, $value) = ($1, $2); $acct =~ s/^\s*//; $acct =~ s/\s*$//; $acct =~ s/\s{3,}[\(\)\d,\.\s]+$//; @@ -71,8 +75,8 @@ my %internalBalances; while (my $line = ) { chomp $line; $line =~ s/^\s*//; $line =~ s/\s*$//; - next unless - $line =~ /^\s*(\S+\:.+)\s+[\(\d].+\s+\(?\s*([\d\.\,])+\s*\)?\s*$/; + die "Strange line, \"$line\" found in ledger output" unless + $line =~ /^\s*(\S+\:[^\$]+)\s+\$?\s*([\-\d\.\,]+)\s*$/; my($acct, $value) = ($1, $2); $acct =~ s/^\s*//; $acct =~ s/\s*$//;