Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Question on Chomp

Reply
Thread Tools

Question on Chomp

 
 
Matt Taylor
Guest
Posts: n/a
 
      09-29-2003
I have this piece of code:

$/ = ' ';
$test = "abc ";
chomp $test;
print $test;

which prints spaces at the end of the line. Shouldn't chomp strip the spaces
off the end of my scalar $test?

-Matt


 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      09-29-2003
Matt Taylor wrote:

> I have this piece of code:
>
> $/ = ' ';
> $test = "abc ";
> chomp $test;
> print $test;
>
> which prints spaces at the end of the line. Shouldn't chomp strip the spaces
> off the end of my scalar $test?


try:

$/ = ' ';
$test = "abc ";
print "$test<\n";
chomp $test;
print "$test<\n";

D:\Snippets>chomp.pl
abc <
abc <

stripping can be done with:

$test =~ s/\s*$//;

ie. remove zero or more (*) white space (\s) form the end ($).

--
Kind regards, prachtige ideeen
John aan het einde van een dal
stromen dagelijks
http://johnbokma.com/ gedachtenwaterval

 
Reply With Quote
 
 
 
 
Matt Taylor
Guest
Posts: n/a
 
      09-29-2003
"John Bokma" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Matt Taylor wrote:
>
> > I have this piece of code:
> >
> > $/ = ' ';
> > $test = "abc ";
> > chomp $test;
> > print $test;
> >
> > which prints spaces at the end of the line. Shouldn't chomp strip the

spaces
> > off the end of my scalar $test?

>
> try:
>
> $/ = ' ';
> $test = "abc ";
> print "$test<\n";
> chomp $test;
> print "$test<\n";
>
> D:\Snippets>chomp.pl
> abc <
> abc <
>
> stripping can be done with:
>
> $test =~ s/\s*$//;
>
> ie. remove zero or more (*) white space (\s) form the end ($).
>
> --
> Kind regards, prachtige ideeen
> John aan het einde van een dal
> stromen dagelijks
> http://johnbokma.com/ gedachtenwaterval
>


Ah. Reading through perlfunc it sounded like chomp ate every trailing
string. I've never really questioned how to use it since I've used it
exclusively to chomp the newlines off of file input.

Thanks for the help.

-Matt


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about using chomp and other functions together George Perl Misc 4 11-16-2009 11:52 PM
Newbie Question (gets.chomp) Guest Ruby 1 07-01-2007 09:46 AM
"chomp,chop" usage i.e. chop immediately after chomp martin Perl Misc 3 04-15-2006 08:09 PM
Gets and chomp method question paul.denlinger@gmail.com Ruby 6 03-28-2006 11:50 PM
Chomp not working properly with Sendmail Aaron Powell Perl 0 07-24-2003 02:24 PM



Advertisments