Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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 contemplating the Monastery: (6)
As of 2014-09-23 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (241 votes), past polls