{"id":8926,"date":"2015-03-07T20:34:32","date_gmt":"2015-03-07T20:34:32","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=8926"},"modified":"2015-03-07T20:34:32","modified_gmt":"2015-03-07T20:34:32","slug":"perl-the-perl-hash","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-the-perl-hash\/","title":{"rendered":"Perl:  the Perl hash"},"content":{"rendered":"<h1>The tutor asks, &#8220;Will the real hash please stand up?&#8221;<\/h1>\n<p>I&#8217;ve been\u00a0 hearing about hashtags for a few years now.\u00a0 At first, I wondered why people were suddenly showing much interest in data structures.<\/p>\n<p>A data structure is a way of storing data that suggests relationships between the entries.\u00a0 An array is a data structure:\u00a0 it stores a list of elements such that each has a position (or index) as well as a value.<\/p>\n<p>In comp sci, another term for <strong>hash<\/strong> is <strong>associative array<\/strong>.\u00a0 A hash stores values not by position, but by <strong>descriptor<\/strong>, also called <strong>key<\/strong>.<\/p>\n<p>In Perl, the hash symbol is %.\u00a0 Here&#8217;s a short Perl program to illustrate a hash:<\/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<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 \/>\nprint &#8220;The capital of $prov is $maritimecapitals{$prov}.\\n\\n&#8221;;<br \/>\n<\/span><br \/>\nLet&#8217;s imagine you call this program <span style=\"font-family:monospace\">maricaps.txt<\/span>, then, from its directory, you key in<br \/>\n<span style=\"font-family:monospace\">perl maricaps.txt<\/span><\/p>\n<p>The program should politely ask you which Maritime province&#8217;s capital you want to know.  Let&#8217;s imagine you key in NS.  Hopefully, you&#8217;ll receive the reply<\/p>\n<p><span style=\"font-family:monospace\">The captial of NS is Halifax.<\/span><\/p>\n<p>There is much more to say about this topic.  Look for more here soon.<\/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>The tutor asks, &#8220;Will the real hash please stand up?&#8221; I&#8217;ve been\u00a0 hearing about hashtags for a few years now.\u00a0 At first, I wondered why people were suddenly showing much interest in data structures. A data structure is a way &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-the-perl-hash\/\"> <span class=\"screen-reader-text\">Perl:  the Perl hash<\/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":[575,577,576,574,173],"class_list":["post-8926","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-associative-array","tag-hash","tag-keys-and-values","tag-perl-hash","tag-perl-programming"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8926","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=8926"}],"version-history":[{"count":12,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8926\/revisions"}],"predecessor-version":[{"id":8938,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8926\/revisions\/8938"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=8926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=8926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=8926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}