You general approach is just fine. However, if you are always reading 2 lines at a time, why not just read them both in the while conditional? If there are an odd number of lines, below code will just throw away that last line that doesn't have a partner following it. The other question is what to do with blank lines at the end? Maybe somebody hit <cr> again a few times? Or mistake in cut-n-paste? I did something simple to allow for an undef resulting from split on "\n". Other implementations are of course possible, I don't know enough about your actual app.

I try to avoid stuff like $tmp[0] because you need a comment to describe what the heck the 0th field actually means! I used an array slice to move the indexing into the split so that I could assign this first column a scalar name. You should use terminology appropriate for your application. I didn't see the need for chomp, only to add a "\n" back in. The split will remove any \n if there is just one token on the line. Also, I personally wouldn't put multiple statements on the same line. Perl doesn't care, but people do.

I would do something different if this involved reading chunks of more than 2 lines. But this specific case, I find the below adequate.

use strict; use warnings; my $cnt=1; while (defined (my $first = <DATA>) and defined (my $second = <DATA>) +) { my $name1 = (split /\s+/, $first)[0] // last; # handle trailing my $name2 = (split /\s+/, $second)[0] // last; # potential "\n" bl +ank lines print "$cnt\t$first"; if ($name1 ne $name2) { $cnt++; } print "$cnt\t$second"; } =prints 1 one 1 one 1 two 1 two 1 three 1 three 1 three 2 four note: same result with or without the trailing blank line =cut __DATA__ one one two two three three three four Odd line here followed by a blank line if you want
Update: Oh, I usually put the numbering on the left. I know how big that column is going to get. Done the other way around, a long token will screwup your nice printout. You didn't specify what is to happen with an odd number of line - it could be "silent ignore" approach above is not what you want - but I couldn't tell.

In reply to Re: Dealing with nextline of a file by Marshall
in thread Dealing with nextline of a file by jnarayan81

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.