{"id":5793,"date":"2014-10-04T18:54:16","date_gmt":"2014-10-04T18:54:16","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=5793"},"modified":"2014-10-04T18:54:16","modified_gmt":"2014-10-04T18:54:16","slug":"perl-the-if-elsif-else-construction","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-the-if-elsif-else-construction\/","title":{"rendered":"Perl:  the if, [elsif], else construction"},"content":{"rendered":"<h1>Tutoring computer science, this topic is fundamental. \u00a0The tutor gives a short program as a focal point.<\/h1>\n<p>&nbsp;<br \/>\nBack in my <a href=\"?p=5774\">last post<\/a> I introduced the <span style=\"font-family:monospace\">if, else<\/span> construction.  Today we&#8217;ll look at a sample program that implements it.<\/p>\n<p>Let&#8217;s imagine that your (geeky) spouse has written a grocery list into a Perl <a href=\"?p=5096\">array<\/a>.  However, you don&#8217;t know if she&#8217;s included olive oil.  You write a little program to read the array, then report if &#8220;olive oil&#8221; is present. <\/p>\n<p>One extra parameter:  your spouse likes to abbreviate.  Therefore, she might have written &#8220;olive o.&#8221; for &#8220;olive oil.&#8221;<\/p>\n<p>Here&#8217;s the program.  Note that the <a href=\"?p=5754\">comments<\/a> are in <span style=\"color:green\">green<\/span>.<br \/>\n<span style=\"font-family:monospace;color:brown\"><br \/>\n<span style=\"color:green\">#!\/usr\/bin\/perl<\/span><br \/>\n<br \/>\n<span style=\"color:green\">#for our purposes, you might imagine you can&#8217;t see the<br \/>#contents of @groceries<\/span><br \/>\n@groceries=(&#8220;butter&#8221;,&#8221;sugar&#8221;,&#8221;bread&#8221;,&#8221;eggs&#8221;,&#8221;deli ham&#8221;,&#8221;cheddar cheese&#8221;,&#8221;olive oil&#8221;,&#8221;coffee&#8221;,&#8221;oranges&#8221;,&#8221;apples&#8221;,&#8221;grapes&#8221;,&#8221;olive o.&#8221;,&#8221;milk&#8221;,&#8221;cream&#8221;);<br \/>\n$i=0;<br \/>\nwhile($groceries[$i]){<br \/>\nif($groceries[$i] eq &#8220;olive oil&#8221;){<br \/>\nprint &#8220;Olive oil is element $i in the list.\\n&#8221;;<br \/>\n}<br \/>\nelsif($groceries[$i] eq &#8220;olive o.&#8221;){<br \/>\nprint &#8220;Olive o. is element $i in the list.\\n&#8221;;<br \/>\n}<br \/>\nelse{<br \/>\nprint &#8220;Not element $i\\n&#8221;;<br \/>\n}<br \/>\n$i++;<br \/>\n}<br \/>\n<\/span><br \/>\nLet&#8217;s imagine this program is called <span style=\"font-family:monospace\">groclist.txt<\/span>.  On the command line, in its proper directory, you&#8217;d run it by keying in<\/p>\n<p><span style=\"font-family:monospace\">perl groclist.txt<\/span><\/p>\n<p>Assuming no typos (by me or you), you might be greeted with the following output:<\/p>\n<p><span style=\"font-family:monospace\"><br \/>\nNot element 0<br \/>\nNot element 1<br \/>\nNot element 2<br \/>\nNot element 3<br \/>\nNot element 4<br \/>\nNot element 5<br \/>\nOlive oil is element 6 in the list.<br \/>\nNot element 7<br \/>\nNot element 8<br \/>\nNot element 9<br \/>\nNot element 10<br \/>\nOlive o. is element 11 in the list.<br \/>\nNot element 12<br \/>\nNot element 13<br \/>\n<\/span><br \/>\n<br \/>\nThe details of this program I&#8217;ll explain next post.  Cheers:)<\/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>Tutoring computer science, this topic is fundamental. \u00a0The tutor gives a short program as a focal point. &nbsp; Back in my last post I introduced the if, else construction. Today we&#8217;ll look at a sample program that implements it. Let&#8217;s &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-the-if-elsif-else-construction\/\"> <span class=\"screen-reader-text\">Perl:  the if, [elsif], else construction<\/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":[243,245,173,244,242],"class_list":["post-5793","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-array-search","tag-if-elsif-else","tag-perl-programming","tag-string-equals","tag-while-loop"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/5793","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=5793"}],"version-history":[{"count":33,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/5793\/revisions"}],"predecessor-version":[{"id":5826,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/5793\/revisions\/5826"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=5793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=5793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=5793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}