
circle
New User
Aug 31, 2008, 9:50 AM
Post #1 of 2
(502 views)
|
|
Array element deletion/splice problem
|
Can't Post
|
|
Hi all! I have this problem that I'm kinda stuck with. I found out after a while that deleting elements in an array while iterating through it was a bad idea...naturally! Doh! I'm kinda fresh on dealing with arrays in this manner. Anyway, I was trying to take on the problem by doing the following, which how ever did not work either, resulting in, saying the least, even more strange results. It is like it is inverted. The routine removes the elements that should be saved and saves those that are supposed to be deleted. Anyway, here's the code, which is part of a subroutine:
foreach my $Row (@AllRows){ if ($Row =~ m/MatchSomeThing/) { push @WantedRows, $Row; #splice(@AllRows, $RowCount, 1); (Obviously doesn't work!) splice(@AllRows, $RowCount, 1, "--Row Processed--\n"); push @RowsToDelete, $RowCount; } $RowCount++; } foreach my $DeleteRow (@RowsToDelete) { splice(@AllRows, $DeleteRow, 1); } At the moment, I'm all blind. Been staring at this for too long. Any advice or hints appreciated. I'm pretty sure it all can be optimized as well. Thanks for your time! Regards /Thomas
|