in reply to Re: recursive loop and performace issue in find::file
in thread recursive loop and performace issue in find::file
Indeed. I think it would have been ok to go on and explain a couple of the situations where it is ok and how to address it:
for my $idx (reverse 0 .. $#array) { my $item = $array[$idx]; if (some_test($item)) { splice(@array, $idx, 1); # remove the element } }
In the above case, it is ok to remove items because of the order they are being processed. Here is another example:
while (@work) { my $item = shift/pop @work; # Possibly push/unshift items onto @work }
In the above case we aren't looping over the array, we are using it as a boolean. When all the items have been removed the loop exists. This is useful when implementing your own stack/queue based solution and avoiding recursion.
I agree with your advice. I am just frustrated when people tell me don't do that unless you know what you are doing. I try to give them examples when it is ok, explain why or examples when it isn't ok and explain why not. I hope you don't mind that I took your advice a little further.
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: recursive loop and performace issue in find::file
by Ratazong (Monsignor) on Apr 27, 2011 at 06:58 UTC |