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

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re^7: Divide array of integers into most similar value halves

Replies are listed 'Best First'.
Re^8: Divide array of integers into most similar value halves
by Pepe (Sexton) on Sep 04, 2008 at 16:04 UTC
    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
        Nice,

        But I found some cases (very few, like 0.001%) where it does not work, ie. @array=(43,44,43);
        It would return (43,43) and (43,44).
        Don't bother if you feel is gonna be a big problem. It's already good and I can just use it in combination with the other scripts that have been dropped in the conversation and choose the best answer.
        My problem is I can't modify it myself due to my low understanding of the code.
        Grrrr...someday.

        Thanks a lot.
        Cheers.
        Pepe.

        I know about FAQ code, but it's very convenient if you can spend the extra computing time and memory, but not the programming time.