lefthanded has asked for the wisdom of the Perl Monks concerning the following question:
Method 2:@a = sort @a; foreach (@a) { ... }
I prefer Method 2 simply because it's slightly less code, but I'm concerned that maybe it's re-sorting @a on each iteration, which would be bad. I notice that the following code runs forever:foreach (sort @a) { ... }
So the loop doesn't fix at its start what it's going to iterate through, which is what makes me think Method 2 might be inefficient. Although maybe the loop decides that it's going to iterate through @a, whatever that may be, whether it changes or not within. So it is fixed in a sense, in which case Method 2 is fine I think, although really now I'm just more confused.my @a = (1); foreach (@a) { push @a, 1; }
|
|---|