use strict; use warnings; use List::Util qw(sum); my $cgs = '100010111010100101011010000101011010111011110101001010101010'; print # prints 45 as does davido's solution sum map substr($cgs, $_->[1], $_->[2]-$_->[1]) =~ tr/0//, map [ split ], ; __DATA__ junk 5 15 junk 23 59 junk 18 34 junk 10 20 junk 9 19 junk 40 49