Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Array Processing

by guha (Priest)
on Oct 08, 2005 at 15:44 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-20 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found