TIMTOWTDI
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11159342 use warnings; use List::Util qw( sum ); local $_ = join '', <DATA>; s/(?:\[ 0, \S+(,.*\n)){2,}/ my $last = $1; '[ 0, ' . (sum $& =~ m~, (\S+),~g) . $last/ge; s/(?:\[ .*(, 0, .*\n)){2,}/ my $last = $1; '[ ' . (sum $& =~ m~\[ (\S+),~g) . $last/ge; print; __DATA__ [ -1, 5, 1 ], [ 0, 5, 1 ], [ 0, 5, 1 ], [ 1, 5, 1 ], [ 3, 4, 1 ], [ 5, 1, 1 ], [ 5, 0, 1 ], [ 5, 0, 1 ], [ 5, 0, 1 ], [ 5, 0, 1 ], [ 5, 0, 1 ], [ 5, 0, 1 ], [ 0, -5, 1 ], [ 0, -5, 1 ], [ 0, -5, 1 ], [ 0, -5, 1 ], [ 0, -5, 1 ], [ 0, -5, 1 ], [ 0, -5, 1 ], [ 0, -5, 1 ], [ 0, -5, 1 ], [ -23, -64, 0 ], [ -5, 0, 1 ], [ -5, 1, 1 ],
which outputs:
[ -1, 5, 1 ], [ 0, 10, 1 ], [ 1, 5, 1 ], [ 3, 4, 1 ], [ 5, 1, 1 ], [ 30, 0, 1 ], [ 0, -45, 1 ], [ -23, -64, 0 ], [ -5, 0, 1 ], [ -5, 1, 1 ],
In reply to Re: Summing up duplicate lines
by tybalt89
in thread Summing up duplicate lines
by oko1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |