{"id":8055,"date":"2015-01-27T19:32:47","date_gmt":"2015-01-27T19:32:47","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=8055"},"modified":"2015-01-27T19:32:47","modified_gmt":"2015-01-27T19:32:47","slug":"perl-programming-chop-and-chomp","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-programming-chop-and-chomp\/","title":{"rendered":"Perl programming:  chop and chomp"},"content":{"rendered":"<h1>Continuing with Perl programming, the tutor discusses a couple of simple yet important functions: chop and chomp.<\/h1>\n<h1><\/h1>\n<p>Back in my <a href=\"?p=6431\">Nov 19 post<\/a>, I mentioned the Perl function <span style=\"font-family:monospace\">chop<\/span>.  In that case, <span style=\"font-family:monospace\">chop<\/span> was used to strip the newline off the end of a name the user had inputted from the keyboard.<\/p>\n<p>The <span style=\"font-family:monospace\">chop<\/span> function will &#8220;chop&#8221; off the last character of a string.  Here&#8217;s an example:<br \/>\n<span style=\"color:brown;font-family:monospace\"><br \/>\n#!\/usr\/bin\/perl<br \/>\n$stringvar=&#8221;You look pretty:)&#8221;;<br \/>\nprint $stringvar;<br \/>\nprint &#8220;\\n\\n&#8221;; <span style=\"color:green\">#Prints two empty lines for easier reading.<\/span><br \/>\nchop $stringvar;<br \/>\nprint $stringvar;<br \/>\n<\/span><\/p>\n<p>should give the output<\/p>\n<p><span style=\"font-family:monospace\">You look pretty:)<\/span><\/p>\n<p><span style=\"font-family:monospace\">You look pretty:<\/span><\/p>\n<p>Notice that, with no newline at the end of the string, <span style=\"font-family:monospace\">chop<\/span> removes the mouth of the smiley.  The <span style=\"font-family:monospace\">chop<\/span> function always removes the last character, whether you want it gone or not.<\/p>\n<p>On the other hand, the <span style=\"font-family:monospace\">chomp<\/span> function (apparently) removes only unseen characters.  Therefore, it will remove a newline from the end of a string, but not a letter, number, or punctuation.  Observe:<br \/>\n<span style=\"color:brown;font-family:monospace\"><br \/>\n#!\/usr\/bin\/perl<br \/>\n$newstringvar=&#8221;I love your purse:) \\n&#8221;;<br \/>\n$question=&#8221;Where did you get it?&#8221;;<br \/>\nprint $newstringvar;<br \/>\nprint $question;<br \/>\nchomp $newstringvar;<br \/>\nchomp $question;<br \/>\nprint &#8220;\\n\\n&#8221;;<span style=\"color:green\">#Prints two empty lines for easier reading.<\/span><br \/>\nprint $newstringvar;<br \/>\nprint $question;<br \/>\n<\/span><\/p>\n<p>should hopefully produce the output<\/p>\n<p><span style=\"font-family:monospace\">I love your purse:)<br \/>\nWhere did you get it?<\/span><\/p>\n<p><span style=\"font-family:monospace\">I love your purse:) Where did you get it?<\/span><\/p>\n<p>The <span style=\"font-family:monospace\">chomp<\/span> function benignly left the second string alone, since its end character is a question mark. From the first string <span style=\"font-family:monospace\">chomp<\/span> did remove the newline character (denoted by <span style=\"font-family:monospace\">\\n<\/span>).<\/p>\n<p>Believe it or not, there is even more about <span style=\"font-family:monospace\">chop<\/span> and <span style=\"font-family:monospace\">chomp<\/span>.  Therefore, I&#8217;ll be saying more about them in future posts:)<\/p>\n<p>Source:<\/p>\n<p>McGrath, Mike.  <em>Perl in easy steps<\/em>.  Southam:  Computer Step, 2004.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continuing with Perl programming, the tutor discusses a couple of simple yet important functions: chop and &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-programming-chop-and-chomp\/\"> <span class=\"screen-reader-text\">Perl programming:  chop and chomp<\/span> Read more \u2192<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[464,463,173],"class_list":["post-8055","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-chomp","tag-chop","tag-perl-programming"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=8055"}],"version-history":[{"count":27,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8055\/revisions"}],"predecessor-version":[{"id":8082,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8055\/revisions\/8082"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=8055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=8055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=8055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}