how to skip new line character

------=_NextPart_000_0015_01C49666.30D23690
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi All
As a beginner in PERL, I wrote a small program which reads data from the =
file and stores in an array. In that process i wanted to skip the new =
line character...

for ex: In my program

say "a.txt" contains

man
pan

tan

In the program

open INPUT,"a.txt" or die $!;
my @file =3D <INPUT>;

when I print the file, I could see $file[2] and $file[4] has only new =
line character...

So while I store in the array I should skip this character..I tried chop =
and chomp..Not effective....

Thanks
Anish


------=_NextPart_000_0015_01C49666.30D23690--

0
anish
9/9/2004 6:41:52 AM
📁 perl.beginners
📃 29388 articles.
⭐ 4 followers.

💬 2 Replies
👁️‍🗨️ 702 Views

---------- Forwarded message ----------
From: David le Blanc <[email protected]>
Date: Thu, 9 Sep 2004 16:54:56 +1000
Subject: Re: how to skip new line character
To: "Anish Kumar K." <[email protected]>

Is the problem either
1)  Remove the end of line character from all lines
2) Remove the end of line character from some lines

or

3) remove the end of line character from all lines AND leave out blank lines.

The easiest solution for (3) is

open INPUT,"a.txt" or die $!;
> my @file = <INPUT>;

my @file=grep !/^$/,<INPUT>;   <-- grep out all the empty lines
chomp(@file);                          <-- remove all newlines.

this can be abbreviated to
chomp( @file=grep !/^$/,<INPUT> );

BUT NOT,  @file=chomp(<INPUT>); since chomp does for return the
chomp'ed data as a result.




On Thu, 9 Sep 2004 12:11:52 +0530, Anish Kumar K.
<[email protected]> wrote:
> Hi All
> As a beginner in PERL, I wrote a small program which reads data from the file and stores in an array. In that process i wanted to skip the new line character...
>
> for ex: In my program
>
> say "a.txt" contains
>
> man
> pan
>
> tan
>
> In the program
>
> open INPUT,"a.txt" or die $!;
> my @file = <INPUT>;
>
> when I print the file, I could see $file[2] and $file[4] has only new line character...
>
> So while I store in the array I should skip this character..I tried chop and chomp..Not effective....
>
> Thanks
> Anish
>
>
0
dmleblanc
9/9/2004 6:55:17 AM
open F, "file.txt";
my @file = <F>;
chomp @file;

print "@file";
Is that what you want ?

----- Original Message ----- 
From: "Anish Kumar K." <[email protected]>
To: <[email protected]>
Sent: Thursday, September 09, 2004 2:41 PM
Subject: how to skip new line character


Hi All
As a beginner in PERL, I wrote a small program which reads data from the
file and stores in an array. In that process i wanted to skip the new line
character...

for ex: In my program

say "a.txt" contains

man
pan

tan

In the program

open INPUT,"a.txt" or die $!;
my @file = <INPUT>;

when I print the file, I could see $file[2] and $file[4] has only new line
character...

So while I store in the array I should skip this character..I tried chop and
chomp..Not effective....

Thanks
Anish



0
perl
9/9/2004 8:17:49 AM
Reply: