#!/usr/bin/env perl # # add - add numbers found in input # # $Id$ # replaces the numbers with cumulative totals, per column # # this is useful - try 'ls -s | add' for a demo # I probably want more flexibility as to what it adds and what it doesn't use warnings; use strict; my %total; $| = 1; # force output line flushing while (<>) { my @line = split(/(-?\d+)/); if ( scalar(@line) < 2 ) { print; next; } for ( my $i = 0, my $j = ( $line[0] =~ /\d/ ) ? 0 : 1 ; $j <= $#line ; ++$i, ++$j, ++$j ) { $line[$j] = ( $total{$i} += $line[$j] ); } print join( '', @line ); }