Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: An efficient way to remove an element from an array

by AnomalousMonk (Canon)
on Aug 06, 2012 at 19:10 UTC ( #985801=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985801]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2016-09-01 03:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (439 votes). Check out past polls.