rjc33 has asked for the wisdom of the Perl Monks concerning the following question:
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:
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!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;
|
|---|