So this is what I believe you currently have , expressed as partitions (for ease of thinking/debugging/vocabulary)

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; my @want = (11, 9, 10); #~ sum(0, 3, 8), #~ 9, #~ sum(5, 2, 0, 2, 1) my @bad_ari = (0, 3, 8, 9, 5, 2, 0, 2, 1); my $min = 5; # minimal "goodness" my @partition = []; for my $val ( @bad_ari ){ push @{ $partition[-1] }, $val; if( $val >= $min ){ push @partition, []; } } dd( \@partition ); ## [[0, 3, 8], [9], [5], [2, 0, 2, 1]] __END__

So compared to what you got that was wrong (11, 9, 5, 4) you can see that 2,0,2,1 doesn't add up to four it adds up to five, so you have off by one error in your c style for loop

Also, like Lennotoecom says , maybe what you want is wrong as well .... I wouldn't know :)


In reply to Re: sum towards nearest good neighbor at either end of numerical array (partition off by one ) by Anonymous Monk
in thread sum towards nearest good neighbor at either end of numerical array by rgart

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.