Unfortunally, if the array is large, and there's a significant number of elements "\n" in the array, the repeated splicing can be slow. splice() is an
O(n) operation (with
n the length of the array), and that's a tight worst case bound (splice may require shifting half the array). If you have a large array, and cannot afford to copy, you can still remove the offending elements in situ:
my $j = 0;
for (my $i = 0; $i < @molecules; $i ++) {
$molecules[$j++] = $molecules[$i] unless $molecules[$i] eq "\n";
}
splice @molecules, $j;