I have always felt the lack of a special variable (call it $LOOPCOUNTER), which starts from 0, and counts the number of times the innermost while, for, etc... has run.
By your definition 'the number of times the innermost ... has run', I'd expect much different behaviour than what you're describing:
There would only be one such variable, so you could only use it in the innermost loop. Any loop would reset it to zero when starting, and increment it at each iteration.
You see, you specifically call out the 'innermost' loop, but I frequently have nested loops, and if I'm interested in the 'number of times the innermost (loop) has been called', I'm interested in the TOTAL number of times it's been called:
my $count = 0;
foreach my $arrayRef ( @twoDimArray ) {
foreach my $item ( @$arrayRef ) {
$count++;
}
}
I'd also be concerned with the 'you could only use it in the innermost loop'. What happens if I use it, in a rather long loop, and someone comes in and adds a line that uses 'foreach' somewhere within the loop? Would it break my code? If so, it's not worth taking the risk of using it.
How would a subroutine that contains a loop affect the restriction on only innermost loops?
...
Personally, if I were to ever use something like this, I would want :
- It to be localized, where it always works for the given loop that I'm in.
- There two be two variables, one for the current number of times through the loop, and one for the total number of times through that loop.
- There be consistent behaviour in how next and redo affect the counter, as well as what the value is when in a continue block.
- There be consideration given to what the affect is when combining this variable with goto
- There be consistent behaviour when using this variable with blocks of code that might be evaluated multiple times. (eg, could this be used to tell how many times you've entered a given function? Iterations within map or grep?)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.