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 :


In reply to Re: Automatic Loop Counter not in perl by jhourcle
in thread Automatic Loop Counter not in perl by b4swine

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.