Hi, All -

Here's a question that I cannot think of an elegant answer for and hope you may be able to help:
$my counter = 0; while ( <> ) { $counter ++; print; }

Now, if this loop is in the body of my programme, it should simply count the lines in a file handed over as an invocation argument to this script. However, if I hand over two or more invocation arguments $counter will not reset to zero when reading in the second, third, etc. file.

Therefore my question: Is there an elegant (!) way to reset the variable back to zero for each file to read in? How can I know that Perl is now accessing the next file in the list of invocation arguments? I was thinking along the lines of a special variable that might change its value when accessing the next file or so.

For purely aesthetic reasons I want to avoid having to write something like this:
for my $file in ( @ARGV ) { open FILE, "<", $file; $my counter = 0; while ( <FILE> ) { $counter ++; print; } ... }


Any ideas on this?

Thanks in advance for your help!

Best regards -

Pat

In reply to How to reset a variable for each file inside a while( <> ) loop? by pat_mc

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.