Skip to content

Instantly share code, notes, and snippets.

@n-boy
Created March 30, 2015 08:35
Show Gist options
  • Save n-boy/b5a23d90f3a96392e1dc to your computer and use it in GitHub Desktop.
Save n-boy/b5a23d90f3a96392e1dc to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#################################################
#
# Summarize all numbers
#
# cat file_with_numbers | sum-all-num.pl
#
#################################################
use warnings;
use strict;
use Math::BigInt;
use Math::BigInt::Calc;
#................................................
sub main {
my $file = shift || '-';
my $res = Math::BigInt->new();
my $res2 = Math::BigInt->new();
open my $F, $file or die "Error: $!";
while (my $line = <$F>) {
while ($line =~ /(\d+(?:\.\d+)?)/g) {
#$res->badd($1);
$res->{value} = Math::BigInt::Calc::_add(undef, $res->{value}, Math::BigInt::Calc::_new(undef,$1));
}
}
close $F;
print $res->bstr() . "\n";
}
#................................................
main(shift);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment