Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Split range 0 to M into N non-overlapping (roughly equal) ranges. by roboticus
in thread Split range 0 to M into N non-overlapping (roughly equal) ranges. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-20 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found