in reply to Divide array of integers into most similar value halves

my @array = (8,14,32,29); @new = sort{$a <=> $b}@array;#sorting $i = 'first';#set flag foreach (@new){ if ($i eq 'first'){ push (@subarray1, $_); $i = 'second'; }else{ push (@subarray2, $_); $i = 'first' } } output: @subarray1 = (8,32) # total value 40 @subarray2 = (14,29) # total value 43

Replies are listed 'Best First'.
Re^2: Divide array of integers into most similar value halves
by moritz (Cardinal) on Sep 02, 2008 at 11:05 UTC
    This is not a general solution. Instead of solving the problem, it puts every second item into each set.

    That happens to work in this case by chance, but you shouldn't rely on the ordering of the input data.