chop函数
Perl's 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.
Perl的 chop和chomp功能通常会引起混乱。 他们不仅听起来相似,而且做类似的事情。 不幸的是,有一个关键difference- 斩删除的最后一个字符的字符串完全,而格格只删除最后一个字符,如果它是一个换行符 。
Chomping $myName cuts off the last newline, leaving just Jacob. Once it's been chomped, further chomping won't do anything at all. Chopping the name, however, will result in the last character being removed, leaving Jaco:
砍掉$ myName会切断最后一个换行符,只剩下Jacob 。 一旦被切碎,进一步的切碎根本不会做任何事情。 但是, 砍掉名字将导致最后一个字符被删除,留下Jaco :
Chomping and chopping an array results each element being acted on, and can be a real time saver.
格格荷兰国际集团和CHOP平安阵列结果上所作用的每个元素,并可以是一个真正的节省时间。
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:
所以请记住- 斩掉最后一个字符,毫无疑问或后悔。 Chomp仅删除了换行符,而字符串本身保持不变。 默认情况下, Chomp不会删除所有空格字符。 实际上,默认情况下,chomp仅删除当前定义为$ INPUT_RECORD_SEPARATOR的内容 。 如果您的目标是修剪字符串末尾的所有空白,请尝试使用由读者提交的正则表达式,例如:
翻译自: https://www.thoughtco.com/perl-array-chop-chomp-function-tutorial-2641180
chop函数