ccelt09 has asked for the wisdom of the Perl Monks concerning the following question:

Hello wonderful Perlmonks! I created a large number of files with the data layout shown below. I foolishly didn't anticipate the problem of having a newline between each line of text.

0 26 2 2699645 1 3 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 21 7 2699676 3 1 1 1 3 1 3 1 1 3 1 1 1 1 3 1 1 3 1 1 3 1 1 1 1 1 1 1 0 14 14 2699968 1 1 3 3 1 1 1 3 3 1 3 1 1 3 1 3 3 1 1 3 1 3 3 3 1 1 3 +3 0 13 15 2700027 1 1 3 3 1 1 1 3 3 1 3 1 1 3 1 3 3 1 1 3 1 3 3 3 3 1 3 +3 0 26 2 2700157 1 3 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 17 11 2700202 3 3 1 1 3 3 3 1 1 3 3 1 1 1 3 1 3 3 1 1 3 1 1 1 1 1 1 +1 0 18 10 2700302 3 1 1 1 3 1 3 3 1 3 3 1 1 1 3 1 3 3 1 1 3 1 1 1 1 1 1 +1 0 15 13 2700608 1 3 3 3 1 3 1 1 3 1 1 3 1 3 1 3 1 1 1 3 1 1 1 3 1 3 3 +3 0 15 13 2700613 1 1 3 3 1 3 1 1 3 1 1 3 1 3 3 3 1 1 1 3 1 1 1 3 1 3 3 +3

The problem this yields is in my next set of files. The blank lines return values of 0 each time they are encountered

2699645 2 0 2699676 7 0 2699968 14

Is there a way to program out these new lines between strings , in addition to the split command for single spaces? Below is my relevant code for the split command.

while($count < 155){ my $output_file = "$output_dir"."compute_minor_alleles_28_males.". +"$count".".txt"; open(OUT, ">$output_file"); print OUT "position\tminor allele frequency\n"; my $input_file = "$input_dir"."temp_file_". "$count."."txt"; open (CG, "<$input_file") or die "Cannot open $input_file:$!\n"; while (my $cg = <CG>){ if ($cg =~ /NA/){ next; } chomp($cg); #get rid of newline character @info = split(" ", $cg);

Replies are listed 'Best First'.
Re: Getting Rid of Newlines Between Strings
by choroba (Cardinal) on Aug 12, 2013 at 23:07 UTC
    You are skipping lines that contain NA. You can skip the empty lines in the same way, just change line 12 to
    if ($cg =~ /^$|NA/) {

    It tells Perl to skip the line if it starts (^) and ends ($) immediately, or (|) contains NA.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Just a thought; to cover the possibility of a humanly blank line which happens to contain whitespace, it could be further modified:
      if ($cg =~ /^\s*$|NA/) {

      This worked perfectly, many thanks for the help. Smooth sailing ahead for the rest of the afternoon!