Hello GrandFather,

Instead you need a while loop which loops while a condition is true. Actually, you need just a little more work than that. You should loop until the file read returns undef.

Well, according to perlsyn#Compound-Statements, “The while statement executes the block as long as the expression is true.” So far, so good. But if that were the whole story, this:

#! perl use strict; use warnings; print "Contents of \$/: |$/|\n"; print ">$_<\n" while <DATA>; __DATA__ 7 42 0

would print only the first two lines, since the final line — which is not terminated by a newline — is '0' and therefore false. But the final line is printed, because with the <> operator (or its equivalent readline call) the while condition is tested for definedness, not truth. This is documented in perlop#IO/Operators:

In these loop constructs, the assigned value ... is then tested to see whether it is defined. The defined test avoids problems where the line has a string value that would be treated as false by Perl; for example a "" or a "0" with no trailing newline.

All of which is (apparently) undercut by the op’s assertion, below, that adding an explicit test for definedness solved his problem. I don’t know how to explain that. An early version of Perl? A strange value for $/? I do know that perlop#IO/Operators says the following are equivalent:

while (defined($_ = <STDIN>)) { print; } while ($_ = <STDIN>) { print; } while (<STDIN>) { print; }

and immediately goes on to imply that explicit assignment to a lexical variable “behaves similarly” to implicit assignment to the default variable, $_.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^2: For loop not executing all the values by Athanasius
in thread For loop not executing all the values by parthodas

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.