Hi,

I have a tab-delimited file in which some lines are completely blank, some have an "a" in the first column and others have an "s" in the first column. I want to print all lines to an output file, except in cases where one or more "s" lines directly follow another "s" line - if this happens then I only want to print the first "s" line. I thought the best way to do this would be to loop through two lines at a time and say unless both the current line and the previous line start with a "s" the line should be printed. I've played with many different ways of writing this, but something like this is probably simplest:

use warnings; use strict; open (IN, "in.txt") or die; open (OUT, ">out.txt") or die; my $previous_line = <IN>; while (my $current_line = <IN>) { chomp for ($previous_line, $current_line); unless ( ($current_line =~ /^s/) and ($previous_line =~ /^s/) ) { print OUT "$current_line\n"; } } close IN; close OUT;
This prints all of the lines in the file, would be very grateful if someone could explain why? If there's a completely different way to tackle this then it would be great to hear as well, I'm new to coding so this just seemed the most logical way to do it. Cheers!

In reply to Issue with unless statement with two conditions acting on two lines by rjc33

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.