Question on Chomp

Discussion in 'Perl' started by Matt Taylor, Sep 29, 2003.

  1. Matt Taylor

    Matt Taylor Guest

    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
    Matt Taylor, Sep 29, 2003
    #1
    1. Advertising

  2. Matt Taylor

    John Bokma Guest

    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
    John Bokma, Sep 29, 2003
    #2
    1. Advertising

  3. Matt Taylor

    Matt Taylor Guest

    "John Bokma" <> wrote in message
    news:...
    > 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
    Matt Taylor, Sep 29, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Powell
    Replies:
    0
    Views:
    1,198
    Aaron Powell
    Jul 24, 2003
  2. Replies:
    6
    Views:
    118
    Guillaume Benny
    Mar 29, 2006
  3. Guest

    Newbie Question (gets.chomp)

    Guest, Jul 1, 2007, in forum: Ruby
    Replies:
    1
    Views:
    102
    Axel Etzold
    Jul 1, 2007
  4. martin
    Replies:
    3
    Views:
    179
    Joe Smith
    Apr 15, 2006
  5. George
    Replies:
    4
    Views:
    99
Loading...

Share This Page