Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Array Cleaning

by Necos (Friar)
on May 02, 2003 at 20:35 UTC ( [id://255159]=note: print w/replies, xml ) Need Help??


in reply to Array Cleaning

There is one more way to deal with the splicing problem: run through the array backwards. It sounds wierd, but it does work. If you build up a list of the indexes that you want to splice out, process it in reverse (using pop), and then splice, the indices won't change (because the array is shrinking from the end), and everything is fine. I just so happened to get bitten by something similar.

Consider the following (untested) snippet:
my $splice_index = 0; my @splice_list; for (@clean) { if (length($_) == 0) { #zero - length string push(@splice_list,$splice_index); $splice_index++; next; } $splice_index++; } #now we have our splice list built up. so, we take care of the element +s that need to go. while (@splice_list) { my $index = pop(@splice_list); splice(@clean,$index,1); } print "$_\n" for @clean; #should produce the correct results @clean = map {s/\s+//g; $_ } @clean; #i try to avoid map in void conte +xt, except for in my sig ^_~ print "$_\t" for @clean; print "\n";
I actually used something like this in a module I'm writing. I hope it's of some use to you. YMMV.

Theodore Charles III
Network Administrator
Los Angeles Senior High
email->secon_kun@hotmail.com
perl -e "map{print++$_}split//,Mdbnr;"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-18 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found