in reply to Re^4: Divide array of integers into most similar value halves
in thread Divide array of integers into most similar value halves

Sorry. I never got back to checking the fix properly.

Would you please try replacing the last line with this (note:pre-decrement not post-decrement).

return \@best, [ grep{ !exists $seen{ $_ } or !--$seen{ $_ } } @$a +Ref ];

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^6: Divide array of integers into most similar value halves
by Pepe (Sexton) on Sep 04, 2008 at 15:07 UTC
    Sorry
    It seems to do worse. Some of the elemenents appear if both of the resulting subarrays

      Grr! Intersection & difference of arrays is something that ought to be in core.

      Looked in List::MoreUtils to no avail.

      I looked at cpan and downloaded Array::Utils but it can't handle duplicates.

      Looked at List::Compare, but it comes complete with the kitchen sink & waste disposal (aliased as Belfast Sink & waste bin) and calculates 20 different things under the guise of "initalising".

      Could you post an example of input array and expected outputs please.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Sure!!! If
        @array = (40,17,40,30,40,25,40); is given

        something like

        @subarray1 = (40,40,30)
        @subarray2 = (40,40,17,25)

        should be returned.

        I guess you already have your own sources, but for intersection of arrays I always use a function from PerlFAQ 4:
        @union = @intersection = @difference = (); %count = (); foreach $element (@array1, @array2) { $count{$element}++ } foreach $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference } +, $element; }

        Let me know if you need more examples, or what kind of them are you looking for