I have an array of array references or two dimensional array called paths. It can contain anywhere from 2 - 163888 array references, and each reference points to an array with anywhere from 2 - 12 elements. The code below works but when paths contains a lot of references it is too slow. My purpose is to remove arrays that have all their elements in another array. For example given

@array1 = [A,B,F,G] @array2 = [A, B, C, D, E, F, G]

then array1 should be removed because all of its elements are in array2. My strategy was to sort paths by the size of the arrays ( smallest to largest ) and then for each array loop over all the other arrays to check if the smaller array is contained in a larger array. As soon as we find that it is contained in another array we remove it, and then check the next smallest array, and so on. But 32488 arrays is a lot to go over like this. I need a faster way. If someone knows how to do this I'd really appreciate it. thanks. - Robert

my @filtered; @paths = sort { @$a <=> @$b } @paths; LINE: for ( my $i = 0; $i < scalar @paths; $i++ ) { my $path = $paths[$i]; my %nodes; @nodes{@$path} = (); for ( my $j = $i + 1; $j < scalar @paths; $j++ ) { my $path_b = $paths[$j]; my $c = grep { exists $nodes{$_} } @$path_b; next LINE if $c == scalar @$path; } push @filtered, $path; }

In reply to Fast Way to find one array in second array by melmoth

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.