in reply to Deleting records from an array

You can delete elements from an array using splice. But it's not a good idea to do that in a loop (very error-prone). Deleting elements from the middle of a big array is not a fast operation anyway. I'd rather replace processed elements with undef. Like that:
use strict; use warnings; use Data::Dumper; my %hash = ( a => 1, n => 1, z => 1, ); my @ary = qw( a b n m y z ); for my $key ( keys %hash ) { for my $elem (@ary) { next if not defined $elem; if ( $elem =~ $key ) { $elem = undef; } } } print Dumper \@ary;
output:
$VAR1 = [ undef, 'b', undef, 'm', 'y', undef ];
It works because $elem in this kind of loop is magical... it's an 'alias' (so I guess a pointer) to the real elements of array.

Replies are listed 'Best First'.
Re^2: Deleting records from an array
by Anonymous Monk on Dec 22, 2014 at 08:37 UTC
    ...if that's still not fast enough, you should think of a better approach, such as grouping your records as you read them (from a file?) in another hash (group by fourth field?).
      ...another optimization would be to use index instead of regex, if your keys are simple literals and not actually regular expressions...