in reply to Re^2: Deleting specific array elements
in thread Deleting specific array elements

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;

Replies are listed 'Best First'.
Re^4: Deleting specific array elements
by matrixmadhan (Beadle) on Nov 26, 2008 at 15:50 UTC
    for (my $i = 0; $i < @molecules; $i ++) {

    Should n't that be as

    for (my $i = 0; $i < scalar(@molecules); $i ++) {
      Why? < provides scalar context to its operands.
        Thanks for that and am not aware of such a context reasoning

        One more question
        for (my $i = 0; $i < @molecules; $i ++) {
        Instead of the above
        my $num_elements = scalar(@molecules); for (my $i = 0; $i < $num_elements; $i++) {

        will that bring any performance improvement or the number of elements of an array is stored internally ( in some scalar ) and there is no need to evaluate it again ?

        Thanks again for the reply! :)