{"id":8939,"date":"2015-03-08T17:33:52","date_gmt":"2015-03-08T17:33:52","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=8939"},"modified":"2015-03-08T17:54:33","modified_gmt":"2015-03-08T17:54:33","slug":"perl-while-loop","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-while-loop\/","title":{"rendered":"Perl:  while loop"},"content":{"rendered":"<h1>This application of the while loop has been long coming.<\/h1>\n<p>Yesterday&#8217;s program, in which the user can indicate a Maritime province and be told its capital, has one drawback:\u00a0 it exits after giving the answer.\u00a0 What if the user wants to be given the capitals of other Maritime provinces?\u00a0 The user needs to recall the program each time.\u00a0 Although not difficult, it&#8217;s not convenient.<\/p>\n<p>With a <span style=\"font-family:monospace\">while<\/span> loop, the user can be invited to play the game over and over again until they decide to stop.<\/p>\n<p>Let&#8217;s look at yesterday&#8217;s program with a <span style=\"font-family:monospace\">while<\/span> loop added.  There are a few other additions to support the while loop.  The new code is in orange:<\/p>\n<p><span style=\"font-family:monospace;color:brown\"><br \/>\n#!\/usr\/bin\/perl<br \/>\n<br \/>\n%maritimecapitals=(&#8216;NB&#8217;,&#8217;Fredericton&#8217;,&#8217;NS&#8217;,&#8217;Halifax&#8217;,&#8217;PEI&#8217;,&#8217;Charlottetown&#8217;);<br \/>\n<span style=\"color:orange\">$prov = &#8216;Y&#8217;;<br \/>\nwhile($prov ne &#8216;X&#8217; &#038;&#038; $prov ne &#8216;x&#8217;){<\/span><br \/>\nprint &#8220;Hello.  Which Maritime capital would you like to know?\\n\\n&#8221;;<br \/>\nprint &#8220;Key in NS for Nova Scotia, NB for New Brunswick, or\\n\\n&#8221;;<br \/>\nprint &#8220;PEI for Prince Edward Island.\\n\\n&#8221;;<br \/>\n$prov=&lt;STDIN&gt;;<br \/>\nchomp $prov;<br \/>\n<span style=\"color:orange\"><br \/>\nif($prov eq &#8216;X&#8217; || $prov eq &#8216;x&#8217;){<br \/>\nnext;<br \/>\n}<\/span><br \/>\nprint &#8220;The capital of $prov is $maritimecapitals{$prov}.\\n\\n&#8221;;<br \/>\n<span style=\"color:orange\">}<br \/>\nprint &#8220;Thanks for playing :)\\n\\n&#8221;;<\/span><br \/>\n<\/span><\/p>\n<p>The <span style=\"font-family:monospace\">while<\/span> condition checks the value of <span style=\"font-family:monospace\">$prov<\/span> to see if it&#8217;s <span style=\"font-family:monospace\">&#8216;X&#8217;<\/span> or <span style=\"font-family:monospace\">&#8216;x&#8217;<\/span>.<br \/>\nIf it is, the program bypasses the code inside the <span style=\"font-family:monospace\">while<\/span>  loop and prints out <span style=\"font-family:monspace\">Thanks for playing:)<\/span><\/p>\n<p>Inside the loop, the value of <span style=\"font-family:monospace\">$prov<\/span> must be checked again because the user freshly enters it.  The code is there, but I&#8217;ll explain it next time, along with a few other details.<\/p>\n<p>HTH:)<\/p>\n<p>Sources:<\/p>\n<p><a href=\"http:\/\/www.sthomas.net\/roberts-perl-tutorial.htm\">Robert&#8217;s Perl tutorial<\/a><\/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>This application of the while loop has been long coming. Yesterday&#8217;s program, in which the user can indicate a Maritime province and be told its capital, has one drawback:\u00a0 it exits after giving the answer.\u00a0 What if the user wants &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-while-loop\/\"> <span class=\"screen-reader-text\">Perl:  while loop<\/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":[173,242],"class_list":["post-8939","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-perl-programming","tag-while-loop"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8939","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=8939"}],"version-history":[{"count":22,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8939\/revisions"}],"predecessor-version":[{"id":8964,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8939\/revisions\/8964"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=8939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=8939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=8939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}