Friday, March 13, 2020
Perl Array chop() and chomp() Function - Quick Tutorial
Perl Array chop() and chomp() Function - Quick Tutorial chop(ARRAY); chomp(ARRAY); Perls chop and chomp functions can often be a source of confusion. Not only do they sound similar, they do similar things. Unfortunately, there is a critical difference- ââ¬â¹chop removes the last character of the string completely, while chomp only removes the last character if it is a newline. $myName Jacob\n; chomp($myName); Chomping $myName cuts off the last newline, leaving just Jacob. Once its been chomped, further chomping wont do anything at all. Chopping the name, however, will result in the last character being removed, leaving Jaco: $myName Jacob; chop($myName); Chomping and chopping an array results each element being acted on, and can be a real time saver. chop(ARRAY); chomp(ARRAY); So remember - Chop chops off the last character without question or regret. Chomp only removed the newline, leaving the string itself intact. Chomp does not remove all whitespace characters by default. In fact, by default, chomp only removes what is currently defined as the $INPUT_RECORD_SEPARATOR. If your goal is to trim all whitespace from the end of your string, try using a regex like this one submitted by a reader: $line ~ s/\s*$//g;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.