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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.