Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by AnomalousMonk (Monsignor)
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


Comment on Re: An efficient way to remove an element from an array
Download Code

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 cooling their heels in the Monastery: (6)
As of 2014-08-22 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (152 votes), past polls