Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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

In reply to chop() and chomp() by Parham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-18 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found