the counter ($lineIndex) in the for loop (c-style) I am using is not progressing from 0. I don't reset it anywhere, and I believe its format is correct within the for loop

The $lineIndex counter is in the inner-loop; are you sure it's not just hitting the conditional and catching the last statement that aborts the loop? This would cause the counter in the inner loop to be 0 the next time it's processed at the next iteration of the outer-loop. Nothing I can see in the provided code should be changing (such as decrementing) the $lineIndex counter.

Perhaps also try printing out the value of your inner loop counter right before calling next to verify it's still set the same as at the loop entry as well, to verify it's the same as when you started as you expect.

Another hint for you, you probably don't want both index() and the regex match against m/$currGB/ since that'll treat the contents of $currGB as a regular expression. Stick with either index(), or look into escaping metacharacters using the quotemeta built-in.

An even more nit-picky style suggestion for you would be to reduce indent level by putting the loop continuation first by reversing the conditional. This is mostly pure style/design preference, but code that needlessly adds to indent level frequently gets harder to maintain in the long-term. Consider:

if ( index($lineinfile, $currGB) == -1 ) { print "Debugging: continuing the loop\n"; next; } # Do your matching workload here.

Update: Sans debugging, you can even use the single line:

next unless ( index($lineinfile, $currGB) != -1 );


In reply to Re: Index isn't progressing in C-style for loop by Apero
in thread Index isn't progressing in C-style for loop by ItsGinny

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.