{"id":6154,"date":"2014-11-02T18:42:26","date_gmt":"2014-11-02T18:42:26","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=6154"},"modified":"2014-11-02T18:42:26","modified_gmt":"2014-11-02T18:42:26","slug":"perl-programming-logical-comparisons-equals-and-string-equals","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-programming-logical-comparisons-equals-and-string-equals\/","title":{"rendered":"Perl programming:  logical comparisons:  equals and string equals"},"content":{"rendered":"<h1>During tutoring, you often need to point out the unexpected details of a language. The tutor brings up a point that is relevant to Perl, but not only Perl&#8230;.<\/h1>\n<p>Back in my<a href=\"?p=5793\"> October 4 post,<\/a> I used a Perl construct that I will explain here now:  specifically, the <strong>eq<\/strong> operator.  There is also potential connection between today&#8217;s post and my one from <a href=\"?p=5774\">October 2<\/a>.  <\/p>\n<p>In Perl, you can compare values for equality (using the double equals comparison operator, <strong>==<\/strong>).  For example:<\/p>\n<p>#!\/usr\/bin\/perl<\/p>\n<p>$joe=$ARGV[0];<\/p>\n<p>$rob=$ARGV[1];<\/p>\n<p>if($rob==$joe){<\/p>\n<p>print &#8220;$rob equals $joe\\n\\n&#8221;;<br \/>\n}<\/p>\n<p>else{<\/p>\n<p>print &#8220;$rob not equal to $joe\\n\\n&#8221;;<br \/>\n}<\/p>\n<p>Let&#8217;s imagine <span style=\"font-family:monospace\">test.txt<\/span> is the above program&#8217;s name.  If it is run from the command line thus:<\/p>\n<p><span style=\"font-family:monospace\">perl test.txt 4 4<\/span><\/p>\n<p>you&#8217;ll likely get the output<\/p>\n<p><span style=\"font-family:monospace\">4 equals 4<\/span>.<\/p>\n<p>If, on the other hand, you run it with this command:<\/p>\n<p><span style=\"font-family:monospace\">perl test.txt 4 5<\/span><\/p>\n<p>you&#8217;ll likely receive the response<\/p>\n<p><span style=\"font-family:monospace\">5 not equal to 4<\/span><\/p>\n<p>So far, all seems pretty straightforward.  However, let&#8217;s run the program again, with this command:<\/p>\n<p><span style=\"font-family:monospace\">perl test.txt coffee tea<\/span><\/p>\n<p>You&#8217;ll likely be greeted with the somewhat controversial statement<\/p>\n<p><span style=\"font-family:monospace\">tea equals coffee<\/span><\/p>\n<p>In Perl, variables that are words (as opposed to numbers) are called strings.  A string is written in quotes, such as &#8220;coffee&#8221;.  A string definition might look like<\/p>\n<p><span style=\"font-family:monospace\">$var1=&#8221;coffee&#8221;;<\/span><\/p>\n<p>Strings, if defined, have a numerical value of &#8220;not zero&#8221;.  The <strong>==<\/strong> operator is a numerical comparison; from its point of view, &#8220;coffee&#8221; and &#8220;tea&#8221; are equal, both being &#8220;not zero&#8221;.<\/p>\n<p>To compare &#8220;coffee&#8221; and &#8220;tea&#8221; as strings, you need to use the operator <strong>eq<\/strong> rather than <strong>==<\/strong>, as follows:<\/p>\n<p><span style=\"font-family:monospace\">if($rob eq $joe)<\/span><\/p>\n<p>If we change the program above, replacing<\/p>\n<p><span style=\"font-family:monospace\">if($rob == $joe)<\/span><\/p>\n<p>with<\/p>\n<p><span style=\"font-family:monospace\">if($rob eq $joe)<\/span><\/p>\n<p>it should be able to distinguish between &#8220;coffee&#8221; and &#8220;tea&#8221;.  If we run it like this:<\/p>\n<p><span style=\"font-family:monospace\">perl test.txt coffee tea<\/span><\/p>\n<p>We should now receive the reassuring response<\/p>\n<p><span style=\"font-family:monospace\">tea not equal to coffee<\/span><\/p>\n<p>Various computer languages treat strings and comparisons in possibly different ways.  The important point is that a given language might have its own, unexpected way of handling them.  Being aware of that possibility, the programmer is better prepared:)<\/p>\n<p><em>Source:<\/em>  <a href=\"http:\/\/www.sthomas.net\/roberts-perl-tutorial.htm\">Robert&#8217;s Perl Tutorial<\/a><\/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>During tutoring, you often need to point out the unexpected details of a language. The tutor brings up a point that is relevant to Perl, but not only Perl&#8230;. Back in my October 4 post, I used a Perl construct &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-programming-logical-comparisons-equals-and-string-equals\/\"> <span class=\"screen-reader-text\">Perl programming:  logical comparisons:  equals and string equals<\/span> Read More &raquo;<\/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":[271,272,173,244],"class_list":["post-6154","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-logical-comparison","tag-numerical-equals","tag-perl-programming","tag-string-equals"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/6154","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=6154"}],"version-history":[{"count":39,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/6154\/revisions"}],"predecessor-version":[{"id":6193,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/6154\/revisions\/6193"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=6154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=6154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=6154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}