in reply to An efficient way to remove an element from an array
Here's an 'efficient' method if you're completely unconcerned about maintaining array order. (This was published by someone else recently in response to a strikingly similar question.)
>perl -wMstrict -le "my @ra = (0, 1, 2, 3, 4, 5); print qq{@ra}; for my $i (2, 0, -2, -1) { $ra[$i] = $ra[-1]; --$#ra; print qq{removed element at index $i: @ra}; } " 0 1 2 3 4 5 removed element at index 2: 0 1 5 3 4 removed element at index 0: 4 1 5 3 removed element at index -2: 4 1 3 removed element at index -1: 4 1
|
|---|