perlquestion
BrowserUk
<blockquote><i></i></blockquote>
<p>I seem to find myself having to do this more an more frequently, but I've never really come up with a clean way to code it. My current method is: <code>
#! perl -slw
use strict;
our $N //= 2;
our $M //= 1e6;
my( $step, $s, @ranges ) = ( int $M / $N, 0 );
push( @ranges, [ $s, $s+$step -1] ), $s+=$step for 1 ..$N;
$ranges[ -1][1] = $M;
printf "%2d : from %7d to %7d (%7d)\n",
$_, @{ $ranges[ $_ ] },
$ranges[ $_ ][ 1 ] - $ranges[ $_ ][ 0 ] + 1
for 0 .. $#ranges;
__END__
[15:43:18.51] C:\test>ranges -N=1 -M=9999997
0 : from 0 to 9999997 (9999998)
[15:43:29.03] C:\test>ranges -N=2 -M=9999997
0 : from 0 to 4999997 (4999998)
1 : from 4999998 to 9999997 (5000000)
[15:43:34.61] C:\test>ranges -N=3 -M=9999997
0 : from 0 to 3333331 (3333332)
1 : from 3333332 to 6666663 (3333332)
2 : from 6666664 to 9999997 (3333334)
[15:43:47.64] C:\test>ranges -N=4 -M=9999997
0 : from 0 to 2499998 (2499999)
1 : from 2499999 to 4999997 (2499999)
2 : from 4999998 to 7499996 (2499999)
3 : from 7499997 to 9999997 (2500001)
[15:43:57.34] C:\test>ranges -N=5 -M=9999997
0 : from 0 to 1999998 (1999999)
1 : from 1999999 to 3999997 (1999999)
2 : from 3999998 to 5999996 (1999999)
3 : from 5999997 to 7999995 (1999999)
4 : from 7999996 to 9999997 (2000002)
</code>
<p>How would you do this?
<div class="pmsig"><div class="pmsig-171588">
<hr />
<font size=1 >
<div>Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.</div>
<div>"Science is about questioning the status quo. Questioning authority". </div>
<div>In the absence of evidence, opinion is indistinguishable from prejudice.</div>
</font>
</div></div>