Jemts has asked for the wisdom of the Perl Monks concerning the following question:

I was reading about the chomp and chop functions and it says chomp is safer than chop and that they do similar things. So my question is when do you use chop insetad of chomp?

Jemts

"If you're traveling in a time machine, and you're eating corn on the cob, I don't think it's going to affect things one way or the other. But here's the point I'm trying to make: Corn on the cob is good, isn't it."

Replies are listed 'Best First'.
Re: When do you use chop instead of comp?
by steves (Curate) on Mar 02, 2002 at 18:45 UTC

    chop always takes off the last character of a string and returns that character.

    chomp takes off any and all trailing newlines, but leaves the string alone if there are no newlines. It returns the number of characters removed. $/ determines what chomp thinks is a newline. So you can local that to remove newlines from files from other systems that have different newlines than your native OS.

          "chomp takes off any and all trailing newlines,..."

      Nope. If there are multiple trailing newlines, chomp only removes one of them. That's the way it works unless you set $/ to an empty string, in which case it gobbles them all.

      Consider:

      my $str = "some string\n\n\n\n"; chomp $str; print "[$str]"; # still three trailing newlines $/ = ""; # 'paragraph mode' chomp $str; print "[$str]"; # all gone
      Note also that while chomp does return the number of characters removed, that result may not be what you think if you are in Windows or Mac OS where \n gets written out as two characters. During the time that Perl still has the string in memory, the \n only counts as one character and the second chomp in the example above returns 3 (not 6) on my Windows machine -- just as it would on Linux.

      On the other hand, if you assign a value to $/ that truly is more than one character (internally), that count gets returned as you would expect.

      $/ = 'ggl'; # giggle mode my $str2 = "a string ggl"; my $cnt = chomp $str2; print "$cnt"; # prints: 3

      ------------------------------------------------------------
      "Perl is a mess and that's good because the
      problem space is also a mess.
      " - Larry Wall

        Thanks. I re-read the docs. Somewhere early on I got the idea it removed all of them. Scary that I never had to check that until now.

Re: When do you use chop insetad of comp?
by data64 (Chaplain) on Mar 02, 2002 at 18:48 UTC

    chomp will only remove any trailing string that corresponds to the current value of $/ usually the "newline" character whereas chop will remove the last character regardless of what it is. The benifit of using chop is that it is much faster since it does not try to scan the string and match the value of $/

    See also chop() and chomp()


    <cite>Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd</cite>

Re: When do you use chop insetad of comp?
by Mandor (Pilgrim) on Mar 02, 2002 at 18:46 UTC
    You could use it in a case where you are absolutely sure that you want to remove the last character of a string.
Re: When do you use chop insetad of comp?
by webadept (Pilgrim) on Mar 02, 2002 at 20:09 UTC
    Chop removes the last character, no matter what it is, EOL, or what ever. Chomp only removes EOL marks if there is one. If not, it does nothing.

    The EOL mark Chomp removes corresponds to the current value of $/, so you may want to check that as well on your system.

    Chomp also returns the total number of characters removed so you get a 1 or a 0 responce. Chop returns nothing.

    webadetp.net
      minor correction. chop returns the character removed.
      $string = "abc"; print chop $string; # prints 'c' print $string; # prints 'ab'

      /\/\averick
      perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

        Ah.. and so it does.. Most usful that. Thanks
Re: When do you use chop insetad of comp?
by Juerd (Abbot) on Mar 02, 2002 at 19:10 UTC
    I would never use chop to remove newline characters, chomp is so much better for it. It can be a handy s/.$//s-shorthand, but you won't need that much :)

    So all things considered, my answer would be: never. Never use chop instead of chomp.

    Lbh ebgngrq guvf grkg naq abj lbh pna ernq vg. Fb jung? :) -- Whreq

Re: When do you use chop insetad of comp?
by shotgunefx (Parson) on Mar 02, 2002 at 20:55 UTC
    I guess an appropriate place to use it would be when reading from STDIN when you are reasonably sure it is line terminated.
    print "Enter a url!:\n"; my $url = <STDIN>; chop($url);
    In practice I NEVER use chop. Even though chomp is slower, I feel that with chop it's a lot easier to introduce subtle bugs. (Let's say you cut and paste a block of code.)

    -Lee

    "To be civilized is to deny one's nature."
      i use chop to rotate.

      my $val = 123; $val = chop($val) . $val; print $val; # prints '312' my $val = 'abc'; $val = chop($val) . $val; print $val; # prints 'cab'

      ~Particle ;Þ

        Pretty cool. Never would have ocurred to me. One of the things I love about Perl (besides making my life so much easier) is that even though I've used it every day for a few years there is still always something more to learn and one more way to do something..

        -Lee

        "To be civilized is to deny one's nature."
Re: When do you use chop instead of chomp?
by dragonchild (Archbishop) on Mar 04, 2002 at 14:35 UTC
    chomp is used for stripping newlines. chop is used when you want to destructively work with the last character of a string. Pretty simple. :-) (In other words, if you're using chop and discarding the return value, maybe you shouldn't be using chop ...)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

A reply falls below the community's threshold of quality. You may see it by logging in.