{"id":6644,"date":"2014-11-27T21:15:50","date_gmt":"2014-11-27T21:15:50","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=6644"},"modified":"2015-02-17T19:13:08","modified_gmt":"2015-02-17T19:13:08","slug":"perl-a-program-to-find-the-median","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-a-program-to-find-the-median\/","title":{"rendered":"Perl:  a program to find the median"},"content":{"rendered":"<h1>Tutoring stats, you discuss the median. \u00a0The tutor introduces a Perl program that finds it.<\/h1>\n<p>I defined the median back in my <a href=\"?p=4174\">May 6<\/a> post.  It&#8217;s preferred over the mean in some realms; I&#8217;ll explore the reason another time.<\/p>\n<p>Today, we&#8217;ll look at a Perl program that finds the median.  It&#8217;s a great way to jump-start the Perl-programming discussion, as well as show off a couple of Perl&#8217;s built-in functions.  Here it is:<\/p>\n<p><span style=\"font-family:monospace;color:brown\"><br \/>\n#!\/usr\/bin\/perl<br \/>\n<br \/>\n@lst=sort{$a <=> $b}@ARGV;<br \/>\n<span style=\"color:orange\">#uses Perl&#8217;s built-in sort to write the numbers, in ascending order, #into @lst<\/span><br \/>\n$n=0;<br \/>\nforeach(@ARGV){<br \/>\n$n++;<br \/>\n} <span style=\"color:orange\">#this loop counts the numbers given to the program<\/span><br \/>\nif($n%2==0){<span style=\"color:orange\">#if the list has an even number of values<\/span><br \/>\n$num1=$lst[$n\/2-1]; <span style=\"color:orange\"><br \/>\n#arrays start at 0, so you need to adjust down one place when #referencing.<\/span><br \/>\n$num2=$lst[$n\/2];<br \/>\n$med=($num1+$num2)\/2;<br \/>\n}<br \/>\nelse{  <span style=\"color:orange\">#the list has an odd number of values<\/span><br \/>\n$med=$lst[($n-1)\/2];<br \/>\n}<br \/>\nprint &#8220;The list you entered is\\n\\n&#8221;;<br \/>\nforeach $one(@ARGV){<br \/>\nprint &#8220;$one &#8220;;<br \/>\n}<br \/>\nprint &#8220;\\n\\n&#8221;;  <span style=\"color:orange\">#makes space for reading<\/span><br \/>\nprint &#8220;The list in ascending order is\\n\\n&#8221;;<br \/>\nforeach $one(@lst){<br \/>\nprint &#8220;$one &#8220;;<br \/>\n}<br \/>\nprint &#8220;\\n\\n&#8221;;<br \/>\nprint &#8220;The median is $med :)&#8221;;<br \/>\nprint &#8220;\\n\\n&#8221;;<\/p>\n<p>Notice the <span style=\"font-family:monospace;color:brown\">%<\/span> on line 9. It&#8217;s the modulus operator, and gives the remainder of a division.  If you divide by 2 and get a remainder of 0, the number must be even.<\/p>\n<p>This program will serve as a platform from which to launch discussions in future posts.  Stay tuned:)<\/p>\n<p>Sources:<\/p>\n<p>McGrath, Mike.  <em>Perl in easy steps<\/em>.  Warwickshire: Computer Step, 2004.<\/p>\n<p><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>Tutoring stats, you discuss the median. \u00a0The tutor introduces a Perl program that finds it. I defined the median back in my May 6 post. It&#8217;s preferred over the mean in some realms; I&#8217;ll explore the reason another time. Today, &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-a-program-to-find-the-median\/\"> <span class=\"screen-reader-text\">Perl:  a program to find the median<\/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":[313,173,312,311],"class_list":["post-6644","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-modulus","tag-perl-programming","tag-perl-sort-function","tag-program-to-find-median"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/6644","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=6644"}],"version-history":[{"count":34,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/6644\/revisions"}],"predecessor-version":[{"id":8488,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/6644\/revisions\/8488"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=6644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=6644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=6644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}