Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Best method to eliminate substrings from array

by jwkrahn (Abbot)
on Jun 26, 2019 at 18:53 UTC ( [id://11101984]=note: print w/replies, xml ) Need Help??


in reply to Best method to eliminate substrings from array

push(@arrCompletedChains, $strChain);

As it says in perlsyn:

If any part of LIST is an array, "foreach" will get very confused if you add or remove elements within the loop body, for example with "splice". So don't do that.

UNTESTED, but you could see if this is faster:

use List::Util 'first'; # sort largest first my @sorted = sort { length $b <=> length $a } @arrWorkingCompletedChai +ns; my %to_delete; # remove shortest first while ( my $next = pop @sorted ) { if ( first { index( $_, $next ) >= 0 } @sorted ) { ++$to_delete{ $next }; } | @arrWorkingCompletedChains = grep !$to_delete{ $_ }, @arrWorkingComple +tedChains;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found