in reply to Re: Summing Variables in foreach loop
in thread Summing Variables in foreach loop
Derived from davido — dunno if it's more elegant or not:
use strict; use warnings; use List::Util qw(sum); my $cgs = '10001011101010010101101000010101101011101111010100101010101 +0'; print # prints 45 as does davido's solution sum map substr($cgs, $_->[1], $_->[2]-$_->[1]) =~ tr/0//, map [ split ], <DATA> ; __DATA__ junk 5 15 junk 23 59 junk 18 34 junk 10 20 junk 9 19 junk 40 49
Update: Changed
map scalar(substr($cgs, $_->[1], $_->[2]-$_->[1]) =~ tr/0//),
to
map substr($cgs, $_->[1], $_->[2]-$_->[1]) =~ tr/0//,
(same result).
|
|---|