Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

sqldatasource dal, wwwxxxenden, convert raid5 to raid 10 mdadm, apache force chunked, nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot

Links

XODOX
Impressum

#1: perl script help

Posted on 2011-10-11 14:31:21 by james varghese

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.

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.


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

Report this message

#2: RE: perl script help

Posted on 2011-10-11 18:42:07 by Ken Slater

> From: james varghese [mailto:james24.v@gmail.com]
> Sent: Tuesday, October 11, 2011 8:31 AM
> To: beginners@perl.org
> Subject: perl script help
>
> 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.
>
> 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.
>

Hi,
Not certain if this is your problem, but you probably want to 'chomp' the
input after reading a line.
Also, what is $k?
You may be able to read the first line outside the loop and print the
header. Then you would not have to worry about the checks for $line_number
(and $k?).
And why do you assign $Line to $_? Seems like an unnecessary step.
As an aside, when referring to the Perl programming language it is usually
capitalized (Perl).
Ken





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

Report this message

#3: Re: perl script help

Posted on 2011-10-11 18:54:56 by Jim Gibson

On 10/11/11 Tue Oct 11, 2011 5:31 AM, "james varghese"
<james24.v@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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#4: Re: perl script help

Posted on 2011-10-13 08:35:51 by james varghese

Thanks for the Suggestions Ken Slater and Jin Gibson.

Sorry for the missing information's.

Exactly what you people predicted is correct.($k is a file counter
starting at zero, and $Line_Counter is a line counter starting at one)

Script is modified according to the suggestions given by you and now
its working fine.

Thanks again,
James



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

Report this message