Question on Chomp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Taylor

    Question on Chomp

    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


  • John Bokma

    #2
    Re: Question on Chomp

    Matt Taylor wrote:
    [color=blue]
    > 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?[/color]

    try:

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

    D:\Snippets>cho mp.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/ gedachtenwaterv al

    Comment

    • Matt Taylor

      #3
      Re: Question on Chomp

      "John Bokma" <postmaster@cas tleamber.com> wrote in message
      news:1064835873 .434654@halkan. kabelfoon.nl...[color=blue]
      > Matt Taylor wrote:
      >[color=green]
      > > 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[/color][/color]
      spaces[color=blue][color=green]
      > > off the end of my scalar $test?[/color]
      >
      > try:
      >
      > $/ = ' ';
      > $test = "abc ";
      > print "$test<\n";
      > chomp $test;
      > print "$test<\n";
      >
      > D:\Snippets>cho mp.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/ gedachtenwaterv al
      >[/color]

      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


      Comment

      Working...