The following preserves output order if desired. Here, the chunk_id value is used for ensuring ordered entry into the @found array. It is important for workers to gather even though sending an empty @results. The reason is that the Manager process must know that a given chunk_id has completed.
use strict;
use warnings;
use MCE::Loop;
use MCE::Candy;
my @patterns = ( "biopattern1", "biopattern2", "biopattern3" );
my $search = join('|', @patterns);
my $regex = qr/$search/;
open my $fh, "gunzip -c in.fastq.gz |" or die "open error: $!";
my @found;
MCE::Loop->init(
max_workers => 4,
chunk_size => 50,
RS => "\n@",
gather => MCE::Candy::out_iter_array(\@found)
);
MCE::Loop->run( sub {
my ( $mce, $chunk_ref, $chunk_id ) = @_;
my @results;
for my $i ( 0 .. $#{ $chunk_ref } ) {
if ( $chunk_ref->[$i] =~ /$regex/ ) {
push @results, $chunk_ref->[$i];
}
}
MCE->gather( $chunk_id, @results );
}, $fh );
MCE::Loop->finish();
close $fh;
print join('', @found);
-
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.
|