BrowserUk:
Hmmm ... I can make it differently, but no cleaner, unfortunately:
$ cat 892828_a.pl
#!/usr/bin/perl
use strict;
use warnings;
my ($M, $N) = (9999997, 3);
my @ranges = map { $_ * int($M/$N) } 0 .. $N-1;
@ranges = map { [ $ranges[$_], $ranges[$_+1]//$M ] } 0 .. $N-1;
print join(", ", map { "(".join("-",@{$_}).")" } @ranges), "\n";
$ perl 892828_a.pl
(0-3333332), (3333332-6666664), (6666664-9999997)
That one gets the second value wrong by one. I tried to find a way to stack the maps together, but couldn't figure out how to do it nicely.
This one gets the values right, but uses an uglier map statement, and still requires the "$ranges[-1][1]=$M" line, which I don't particularly care for:
$ cat 892828_b.pl
#!/usr/bin/perl
use strict;
use warnings;
my ($M, $N) = (9999997, 3);
my $step=0;
my @ranges = map { my ($c,$d)=($step,$_*int($M/$N)); $step=$d; [ $c, $
+d-1 ] } 1 .. $N;
$ranges[-1][1]=$M;
print join(", ", map { "(".join("-",@{$_}).")" } @ranges), "\n";
$ perl 892828_b.pl
(0-3333331), (3333332-6666663), (6666664-9999997)
Perhaps one of these will be close enough to inspire a better solution for you?
...roboticus
When your only tool is a hammer, all problems look like your thumb.
Update: Added code tags to a bit of text so it renders correctly.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.