Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
while (my $test1 = <$fh1>, my $test2 = <$fh2>) This is "comma operator". The "true" or "false" of the "while" only depends upon the state of $test2. The comma operator is a way to combine two what normally would be completely separate statements "together". This should be used only with extreme caution.

A common use for this is in user prompting loops. Putting the prompt together with the ending condition solves the problem of "how to get the loop started" and at the same time puts the condition that is going to stop the loop right up there where it belongs - right in the statement that runs the loop - not buried somewhere in the "guts".

Here is an example. We are asking about part numbers. The user prompt is clear. What stops the loop is clear (q,Q,QuIT, etc) and it is right there where it is supposed to be, in the controlling loop statement. In the "guts" of the loop there will be many error messages like "hey, dummy a part number needs 10 digits" or whatever, followed by a "next;" statement - that restarts the loop and re-prompts the user.

while ( (print "Enter Part No and Qty: "), (my $line=<STDIN>) !~ /^\s*q(uit)?\s*$/i ) {...}
Some would argue that it is better to move the ending condition into the loop and make it the first statement. Well ok thats one way. In that case we have:
last if $line !~/^\s*q(uit)?\s*$/i;
I would argue against that as "do forever" loops should be reserved for servers, GUI event loops and such.

In any event, the OP doesn't appear to be in need of such things.

In reply to Re^2: Reading two text files parallelly... by Marshall
in thread Reading two text files parallelly... by biswanath_c

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (5)
    As of 2018-05-22 18:08 GMT
    Find Nodes?
      Voting Booth?