Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

chop and chomp

by Anonymous Monk
on Mar 17, 2009 at 13:44 UTC ( #751191=perlquestion: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re: chop and chomp
by ikegami (Patriarch) on Mar 17, 2009 at 13:46 UTC

    chop removes the last character of each argument.

    chomp removes the last character(s) of each argument if they're equal to the value of $/.

    chomp is useful in combination with <$fh> because <$fh> reads until the value of $/ or eof is found.

Re: chop and chomp
by Anonymous Monk on Mar 17, 2009 at 13:47 UTC
Re: chop and chomp
by olus (Curate) on Mar 17, 2009 at 13:46 UTC
    perldoc perlfunc
Re: chop and chomp
by manoj_speed (Prior) on Mar 17, 2009 at 13:50 UTC

    chop is used to remove the last character,chomp function removes only line endings.

    chop removes the last character and returns the character chopped. Chomp removes any trailing string that corresponds to the current value of $/ and returns number of characters removed.

      chomp function removes only line endings

      Only for a very specific definition of line endings. That's misleading. It won't even remove a Windows line ending (CRLF) on a Windows machine by default. Nor would you want it to do so.

Re: chop and chomp
by Bloodnok (Vicar) on Mar 17, 2009 at 13:57 UTC
    I suggest you follow and read the links contained in this posting (by AM) - then and only then, feel free to ask further questions - if you have any.

    A user level that continues to overstate my experience :-))
Re: chop and chomp
by apl (Monsignor) on Mar 17, 2009 at 14:13 UTC

      Zap yourself too for linking to a specific PerlMonks domain.

      Better:

      [id://750717|Zzzzzzzzzzzap!]

      Also available:

      [href://?node_id=750717|Zzzzzzzzzzzap!]
      <a href="?node_id=750717">Zzzzzzzzzzzap!</a>

        Hoisted by mine own petard. I am a smoldering corpse at your feet...
Re: chop and chomp
by ELISHEVA (Prior) on Mar 17, 2009 at 21:30 UTC

    Chomp is generally recommended over chop because chop can have unexpected results. For example, suppose all but the last line of a file ends in a new line. chomp will leave the last line intact (since it only removes $/) but chop will remove the last letter of the last line since it blindly removes the last character, whatever it is - probably not what you want.

    Best, beth

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2022-12-02 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?