develooper Front page | perl.beginners | Postings from December 2002

Re: how to delete space at end of line

Thread Previous | Thread Next
From:
Joseph Paish
Date:
December 1, 2002 05:36
Subject:
Re: how to delete space at end of line
Message ID:
0212010635510A.01057@localhost.localdomain
On Friday 29 November 2002 14:28, you wrote:
> Joseph Paish wrote:
> > i have a data file that i am reading into an array.  some of the data
> > file entries have a space at the end of the line and some do not.  is
> > there some way that i can delete the space if it exists as i read each
> > line (something like chomp deletes newlines if they exist)?  preferably
> > something legible to a perl newbie (i am going to have to maintain this
> > in the future, and the less obscure the code, the better).
> >
> > thanks
> >
> > joe
> >
> > ps.  examples of data file entries :
> >
> > first line of file
> > second line of file
> > third line of file
> >
> > the space at the end of the second line is invisible just to look at it,
> > but i need to strip it off as i read it.
>
> If the current line is in $_ (the default):
>
> s/\s+$//;
>
> If instead you have assigned the current line to a variable like $line
>
> $line =~ s/\s+$//;
>
> P.S.  This is a Frequently Asked Question and is answered in the
> perlfaq4 document that is supplied with Perl.
>
> perldoc -q "How do I strip blank space from the beginning/end of a
> string"
>
>
>
> John

thanks to all that replied.  

i guess i have to learn my way around regular expressions now.  i've managed 
to go this long without studying them because i find them to be cryptic (to 
say the least).  at least this one is fairly simple, and by deconstructing it 
piece by piece, i can even understand how it works.

joe

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About