Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Array Processing

by guha (Priest)
on Oct 08, 2005 at 15:44 UTC ( #498422=note: print w/ replies, xml ) Need Help??


in reply to Array Processing

I would look at the problem from another side ;-)

#!perl use strict; use warnings; local $, = "\n"; local $\ = "\n"; my @a = (1..5); foreach (reverse @a){ $a = pop @a; print "Deleted value: $a"; } print "\nDeleted Array Content: ", @a; __DATA__ Deleted value: 5 Deleted value: 4 Deleted value: 3 Deleted value: 2 Deleted value: 1 Deleted Array Content:

Working backwards and pop-ing, I don't change index of the remaining items in the array.

The deletion could even be conditional using splice as in:

#!perl use strict; use warnings; local $, = "\n"; local $\ = "\n"; my @a = (0..5); foreach (reverse @a){ next unless $_ % 2; splice(@a, $_, 1); print "Deleted value: $_"; } print "\nDeleted Array Content: ", @a; __DATA__ Deleted value: 5 Deleted value: 3 Deleted value: 1 Deleted Array Content: 0 2 4


Comment on Re: Array Processing
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://498422]
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: (9)
As of 2015-07-07 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls