#!/usr/bin/perl # use strict; use warnings; my $intervalStr = q{1..25}; my @subIntervalStrs = qw{ 3..12 7..14 11..19 12..17 }; my ( $begin, $end ) = split m{\.\.}, $intervalStr; my @subIntervals = sort { $a->{ min } <=> $b->{ min } } map { my ( $min, $max ) = split m{\.\.}, $_; { min => $min, max => $max }; } @subIntervalStrs; for my $value ( $begin .. $end ) { my $count = 0; shift @subIntervals while @subIntervals and $value > $subIntervals[ 0 ]->{ max }; for my $subInterval ( @subIntervals ) { if ( $value < $subInterval->{ min } ) { last; } elsif ( $value > $subInterval->{ max } ) { next; } else { $count ++; } } printf qq{%4d => %d\n}, $value, $count; }