Reputation: 2951
I have an array, @cuts
, of indices of the elements that I would like to remove from @Data
. Is this an appropriate way to do so?
foreach (@cuts){
$Data[$_] = "NULL";
}
for my $i (0 .. $#Data){
if ($Data[$i] eq "NULL"){
splice(@Data,$i,1);
}
}
Upvotes: 1
Views: 70
Reputation: 385897
You don't need to use a sentinel value ("NULL"
).
my %cuts = map { $_ => 1 } @cuts;
my @keeps = grep !$cuts{$_}, 0..$#Data;
@Data = @Data[@keeps];
This can surely be simplified by merging it with the preceding code.
Upvotes: 3
Reputation: 7912
Combining @toolic and @user2752322:
delete @Data[@cuts];
my @newData = grep { defined } @Data;
Upvotes: 2