Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: What is the most efficient way to split a long string (see body for details/constraints)?

by marioroy (Vicar)
on Jun 20, 2019 at 04:31 UTC ( #11101604=note: print w/replies, xml ) Need Help??


in reply to What is the most efficient way to split a long string (see body for details/constraints)?

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

  • Comment on Re: What is the most efficient way to split a long string (see body for details/constraints)?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11101604]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2019-11-12 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (64 votes). Check out past polls.

    Notices?