Hi mikegold10,

Tonight pondered over this site and came across your post. It's been a while since I last posted here.

MCE's chunking engine is beneficial for your case. Moreover, MCE::Relay makes it possible to run serially and orderly if needed. Notice how the parallel code looks very much like the serial code. The extra bit is that workers loop over the chunk.

use warnings; use strict; use 5.30.0; use MCE::Loop; MCE::Loop::init( max_workers => 4, init_relay => 1, # enables MCE::Relay feature ); mce_loop_f { my ( $mce, $chunk_ref, $chunk_id ) = @_; my $output = ''; for my $line ( @{ $chunk_ref } ) { # Skip lines ending with an empty field next if substr($line, -2) eq "\t\n"; # Remove "\n"; chomp $line; # Split matching lines into fields on "\t", creating @fields my @fields = split /\t/, $line; # Copy only the desired fields from @fields to create a new # line in TSV format # This can be done in one simple step in Perl, using # array slices and the join() function my $new_line = join "\t", @fields[ 2, 3, 12..18, 25..28, 31 ]; # Append to buffer with newline char $output .= $new_line . "\n";; } # The MCE relay takes a code block and runs serially # including orderly, one worker at a time. Orderly is # driven by the chunk_id value behind the scene. # Thus, must call MCE::relay per each chunk. MCE::relay { print $output; STDOUT->flush; }; } \*STDIN; # This signals the workers to exit. # If omitted, called automatically when the script terminates. MCE::Loop::finish;

Regards, Mario


In reply to Re: What is the most efficient way to split a long string (see body for details/constraints)? by marioroy
in thread What is the most efficient way to split a long string (see body for details/constraints)? by mikegold10

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.