views:

742

answers:

5

Hi, I am a noob Perl user trying to get my work done ASAP so I can go home on time today :)

Basically I need to print the next line of blank lines in a text file.

The following is what I have so far. It can locate blank lines perfectly fine. Now I just have to print the next line.

    open (FOUT, '>>result.txt');

die "File is not available" unless (@ARGV ==1);

open (FIN, $ARGV[0]) or die "Cannot open $ARGV[0]: $!\n";

@rawData=<FIN>;
$count = 0;

foreach $LineVar (@rawData)
    {
     if($_ = ~/^\s*$/)
     {
      print "blank line \n";
                    #I need something HERE!!

     }
     print "$count \n";
     $count++;
    }
close (FOUT);
close (FIN);

Thanks a bunch :)

A: 

Add a variable like $lastLineWasBlank, and set it at the end of each loop.

if ( $lastLineWasBlank ) 
   {
   print "blank line\n" . $LineVar;
   }

something like that. :-)

Ron

Ron Savage
+1  A: 

Hi,

I'd go like this but there's probably other ways to do it:

for ( my $i = 0 ; $i < @rawData ; $i++ ){
   if ( $rawData[$i] =~ /^\s*$/ ){
       print $rawData[$i + 1] ; ## plus check this is not null
   }
}

J.

jeje
Doesn't this just print all non blank lines?
Ralph Rickenbach
+2  A: 

Elaborating on Ron Savage's solution:

foreach $LineVar (@rawData)
    {
        if ( $lastLineWasBlank ) 
           {
                print $LineVar;
                $lastLineWasBlank = 0;
           }
        if($LineVar  =~ /^\s*$/)
        {
                print "blank line \n";
                    #I need something HERE!!
                $lastLineWasBlank = 1;
        }
        print "$count \n";
        $count++;
    }
Ralph Rickenbach
I think you want `if($LineVar =~ /^\s*$/)` since you're not using $_ in the loop, are you?
Telemachus
Also, do you want `0` and `1` where you have `false` and `true`? (No built-in booleans in Perl, are there?)
Telemachus
so if I were to use this solution, do I need to put quotations around 'true' and 'false'?
mr.flow3r
Thanks for the corrections, haven't been doing Perl for quite a while
Ralph Rickenbach
@mr.flow3r: No, you can't do it directly with 'true' and 'false', even if you quote them. (Well, unless you then test if `$flag eq 'true'` which is too wordy.) I would use 0 and 1 for simple booleans in Perl.
Telemachus
@Telemachus - oh I see. Thanks for clarification :)
mr.flow3r
+6  A: 
open (FOUT, '>>result.txt');

die "File is not available" unless (@ARGV ==1);

open (FIN, $ARGV[0]) or die "Cannot open $ARGV[0]: $!\n";

$count = 0;

while(<FIN>)
{
    if($_ = ~/^\s*$/)
    {
            print "blank line \n";
            count++;
            <FIN>;
            print $_;

    }
    print "$count \n";
    $count++;
}
close (FOUT);
close (FIN);
  • not reading the entire file into @rawData saves memory, especially in the case of large files...

  • <FIN> as a command reads the next line into $_

  • print ; by itself is a synonym for print $_; (although I went for the more explicit variant this time...

Stobor
what happens with the above in the event where you have two blank lines in a row?presumably you would want to print the line following the 2nd blank line, but this code will skip such a line, will it not?
blackkettle
@blackkettle: very true. If that is a requirement, then Ron/malach's solution is better.
Stobor
thanks a lot for your solution :) luckily my file did not have two blanks in a row.
mr.flow3r
A: 
sh> perl -ne 'if ($b) { print }; if ($b = !/\S/) { ++$c }; END { print $c,"\n" }'

Add input filename(s) to your liking.

reinierpost