On 10/11/11 Tue  Oct 11, 2011  5:31 AM, "james varghese"
<james2...@gmail.com> scribbled:

> hi,
> I am new to perl programming.I am trying with the following script and
> need help for it.
> 
> I consolidated 10 excel files(in .txt format) which has same headers
> in it and so i made it 1 common header at the top.While doing it,in
> final output file i see a blank row at the beginning of every file
> consolidated.I wanted to get rid of it and to be displayed all in a
> single file.

Are you saying that there is a blank line in the consolidated file just
before the first line of each copied file? I will assume so, but please
correct me if I am wrong.

> 
> Sample Script:
> 
> while(<FILE>) {
> if (($k ==0) and ($Line_Counter ==1)) {
> my @F=split(/[\t]+/, $_);
> print CONSOL_FILE "$F[0] \t $F[1] \t $F[2] \n";
> }
> if (($k >=1) and ($Line_Counter>1)) {   # doesnt print headers
>        $Line=$_;
> my @F=split(/[\t]+/, $Line);
> print CONSOL_FILE  "$F[0] \t $F[1] \t $F[2] \n";
> }
> $Line_Counter = $Line_Counter + 1;
> }
> close FILE ;
> note:tab is the delimiter used here.
> 
> Any help is Appreciated.
> 

You have not shown us the whole program, so it is difficult to help you. I
see nothing wrong with the logic of your program, so the problem may lie
with what you have not shown us.

Specifically, what is $k, what is its initial value, and when does the value
change? It looks like a file counter. Also, what is the initial value of
$Line_Counter, and does it get re-initialized for each file read?

Assuming $k is a file counter starting at zero, and $Line_Counter is a line
counter starting at one, you can use the following logic to skip printing
the first line of each file read except for the first:

   if( $k == 0 or $Line_Counter > 1 ) {
      print join("\t",@F);
   }

If you are getting blank lines and just want to eliminate them, then test
for blank lines by putting this line in your while loop:

  next if /^\s*$/;

This will skip any line consisting only of whitespace. If your definition of
blank line differs from that, please let us know what it is.

Other suggestions:

1. Put 'use strict;' and 'use warnings;' in your program.
2. Use the $. built-in variable as a line counter.
3. Use join as shown for printing.
4. Improve your indenting for readability.
5. Be more consistent in naming and using variables, as in $k and
$Line_Counter.

Good luck!



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to