push(@arrCompletedChains, $strChain); #### 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. #### use List::Util 'first'; # sort largest first my @sorted = sort { length $b <=> length $a } @arrWorkingCompletedChains; my %to_delete; # remove shortest first while ( my $next = pop @sorted ) { if ( first { index( $_, $next ) >= 0 } @sorted ) { ++$to_delete{ $next }; } | @arrWorkingCompletedChains = grep !$to_delete{ $_ }, @arrWorkingCompletedChains;