my $zones = reduce { my( $first, $last ) = split '-', $b; $last ||= $first; #allocate $a here $a .= chr(0) x ( $last ); substr( $a, $first, $last - $first + 1 ) = 'x' x ( $last - $first + 1 ); $a } '', split ',', '10-374,376-379,382-385,388-499,530-534,541-543,618,619,700-704,707-709';