Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: remove current element out array

by comatose (Monk)
on May 19, 2000 at 19:28 UTC ( [id://13249]=note: print w/replies, xml ) Need Help??


in reply to remove current element out array

If you want to know the current element in a foreach loop, you could easily switch to a for loop. In this case $i is your array index.

for ($i = 0; $i < scalar @array; $i++) { ... }

Here's something else you could do. As always, there's more than one way to do it.

sub removeJunk { my @stuff = @_; my (@keepers); while (my $value = pop @stuff) { @keepers = (@keepers, $value) if ($value =~ /^test\s/); } return @keepers; }

If you have a really big array, you will want to use references.

Replies are listed 'Best First'.
RE: Re: remove current element out array
by Anonymous Monk on May 20, 2000 at 01:19 UTC
    You wouldn't want to use the for loop with a splice - that would incrememnt the $i counter AND move up all the array's elements, causing an elemnet to be skipped.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-19 22:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found