If your array is too big to copy it, try something like this:
for my $m (reverse 0 .. $#molecules) {
if ($molecules[$m] eq "\n"){
splice (@molecules, $m, 1);
}
}
| [reply] [d/l] |
my $to = 0;
for (@m) {
$m[$to++] = $_ if $_ ne "\n";
}
splice @m, $to;
| [reply] [d/l] |
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;
| [reply] [d/l] |
for (my $i = 0; $i < @molecules; $i ++) {
Should n't that be as
for (my $i = 0; $i < scalar(@molecules); $i ++) {
| [reply] [d/l] [select] |