Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

chop() and chomp()

by Parham (Friar)
on Jan 01, 2002 at 04:23 UTC ( [id://135462]=perltutorial: print w/replies, xml ) Need Help??

documentation provided for the functions:

------------------------
chomp
This is an alternative to the chop() function. It removes characters at the end of strings corresponding to the $INPUT_LINE_SEPARATOR ($/). It returns the number of characters removed. It can be given a list of strings upon which to perform this operation. When given no arguments, the operation is performed on $_.

chop
This function removes the last character of a string and returns that character. If given a list of arguments, the operation is performed on each one and the last character chopped is returned.
------------------------

these two functions are very much alike... they both remove one (or more) characters from the end of a string... So how are they different you ask? Chomp() ONLY removes new line characters (these are specified in $/), whereas Chop() removes anything that is at the end of the string (it really doesn't care what it is)...

let's demonstrate these two functions:

#chomp() EXAMPLES $a = "abcdefghij"; chomp($a); print $a; #would return exact string... nothing to remove $a = "abcdefghij\n"; chomp($a); print $a; #would return 'abcdefghij', removed newline $a = "abcdefghij\n"; $b = chomp($a); print $b; #would return 1, it did remove something for sure #chop() EXAMPLES $a = "abcdefghij"; chop($a); print $a; #this would return 'abcdefghi' $a = "abcdefghij"; $b = chop($a); print $b; #this would return 'j'
remember.. this with a little bit of usefulness chop() can be the same as chomp()
$a = "abcdefghij\n"; if ($a =~ /\n$/) { chop $a; } #this could also be \r\n if on windows p +latform
most of the time, you'll want to chomp(), but you might want to use chop() with regexes for the same output

Replies are listed 'Best First'.
Re: chop() and chomp()
by davorg (Chancellor) on Jan 02, 2002 at 15:39 UTC
    this could also be \r\n if on windows platform

    Did you try this?

    \n is a logical end of line character which is mapped to the correct physical value by Perl's IO library. If this wasn't true, then porting apps from Unix to Windows would be a lot harder than it currently is.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: chop() and chomp()
by Juerd (Abbot) on Jan 01, 2002 at 22:00 UTC
    perlfunc Describes both chop and chomp, with good examples and excellent detailed explanations.
    I downvoted the tutorial, because it adds nothing to the existing documentation and thus only makes things harder for beginners.

    1. Why did you use $a instead of $_?
    2. print Doesn't return it's given attributes, it returns true if succesful. All your "would return" comments are wrong.
    3. You didn't even chomp arrays or hashes.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      it wasn't meant as a horribly detailed tutorial. It was meant as a VERY simple tutorial for those that might have not understood why the functions were used. I had it on the computer and i decided to add it to the 'tutorials' section cuz there were no other tutorials on the subject. I didn't use $_ because it might have very well confused those starting out. When in my comment i used 'return', i did not mean literally, i just meant it would 'output' and i feel the need to chomp arrays or hashes cuz again, i wanted it to be a very simple tutorial. I very well understand why you downvoted the tutorial and hope the explanation i gave helped you understand why i wrote it very basically :).
      I'm new to perlmonks (and somewhat to perl) and I hope this acidic comment is an isolated case concerning reactions to people who post useful tutorial tidbits. I for one understood the original post very well. Granted, I think that total newbies might be a bit confused by "print" returning a string. Still, don't you think it's better providing a clarification in a less ...overbearing... tone? :)
Re: chop() and chomp()
by SamCG (Hermit) on Mar 15, 2006 at 18:09 UTC
    I might add the one bit that made me curious (and that I've never really investigated) you didn't really expand on. Specifically, how the function of "$/" works. Admittedly, it's not something one fools with too often, but a bit of explanation may be in order.

    So, I ran some quick tests just to look at what happens when you modify $/. Some of these have implications even when you don't modify $/ -- for example, only one newline will be removed, even if there are two.
    $/='abc'; $_='lafbabc'; chomp; print; ##prints 'lafb', removes 'abc' as expected $_='lafbabcq'; chomp; print; ## prints 'lafbabcq', does not remove embedded 'abc' $_='lafbab'; chomp; print; ## prints 'lafbab', does not remove partial $/ -- 'ab' $_='lafbc'; chomp; print; ## prints 'lafbc', does not remove partial $/ - 'bc' $_='lafabcabc'; chomp; print; ##prints 'lafabc', only removes ONE $/
    Note that the whole string in $/ needs to be present to be considered an "end-of-line" (it doesn't pick out portions of $/), and that it won't remove embedded $/'s.

    These things I think might be helpful to beginners as examples. Also, you might have included the warning about parentheses
    chomp $a, $b; ## this means chomp $a, but leave $b alone! chomp ($a, $b); ## this means chomp both
    I'm going to reserve upvotes/downvotes -- I think you can improve the tutorial considerably and make it worthy of an upvote.

      Be careful with variables named $a and $b, they are special when used in conjunction with sort.

      For example:

      sort { $b cmp $a } @foo;
      gives you an reverse alphabetical (ASCIIbetical really) sort on @foo.


      TGI says moo

Re: chop() and chomp()
by pikablu (Initiate) on May 25, 2007 at 20:39 UTC
    Ah, I get the use of chomp() now, very useful.
      Really good...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://135462]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-19 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found