{"id":8489,"date":"2015-02-17T20:08:46","date_gmt":"2015-02-17T20:08:46","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=8489"},"modified":"2015-02-17T20:11:21","modified_gmt":"2015-02-17T20:11:21","slug":"perl-alphabetizing-a-list-of-command-line-arguments","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-alphabetizing-a-list-of-command-line-arguments\/","title":{"rendered":"Perl:  alphabetizing a list of command line arguments"},"content":{"rendered":"<h1>The tutor returns to the perl sort function.<\/h1>\n<p>Back in my post on <a href=\"?p=6644\">November 27<\/a> I introduced a perl program that finds the median of a list of numbers.  It depends on perl&#8217;s built-in <strong>sort<\/strong> function to put the numbers in order.<\/p>\n<p>Not only does perl&#8217;s <strong>sort<\/strong> work for numbers, but also for words.  The syntax for numbers is<\/p>\n<p><span style=\"color:brown;font-family:monospace\">sort{$a <=> $b}@array_of_numbers<\/span><\/p>\n<p>while the syntax for words (aka strings) is<\/p>\n<p><span style=\"color:brown;font-family:monospace\">sort{$a cmp $b}@array_of_strings<\/span><\/p>\n<p>The <strong>cmp<\/strong> in the strings syntax is itself a built-in function that compares strings.<\/p>\n<p>Here&#8217;s a brief program that orders a list of words it receives from the command line:<br \/>\n<span style=\"font-family:monospace;color:brown\"><br \/>\n#!\/usr\/bin\/perl<br \/>\n<br \/>\n@array_in_order=sort{$a cmp $b}@ARGV;<br \/>\nprint &#8220;\\n\\nHere is your list of words in alphabetical order:\\n\\n&#8221;;<br \/>\nforeach $one(@array_in_order){<br \/>\nprint &#8220;$one &#8220;;<br \/>\n}<br \/>\nprint &#8220;\\n\\n&#8221;;<\/p>\n<p>Running the program from the terminal, you enter the list of words after the program name with just spaces (not commas) between.  Suppose you save the program as<\/p>\n<p><span style=\"font-family:monospace\">alphasort.txt<\/span><\/p>\n<p>and want to run it on the list <\/p>\n<p><span style=\"font-family:monospace\">apple xylophone holiday beeswax packing<\/span><\/p>\n<p>From the proper directory you key in<\/p>\n<p><span style=\"font-family:monospace\">perl alphasort.txt apple xylophone holiday beeswax packing<\/span><\/p>\n<p>then press <span style=\"font-variant:small-caps\">enter<\/span>.<\/p>\n<p>I&#8217;ll be talking more about the perl sort function, string comparisons, and much else in coming posts.  HTH:)<\/p>\n<p>Source:<\/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 returns to the perl sort function. Back in my post on November 27 I introduced a perl program that finds the median of a list of numbers. It depends on perl&#8217;s built-in sort function to put the numbers &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-alphabetizing-a-list-of-command-line-arguments\/\"> <span class=\"screen-reader-text\">Perl:  alphabetizing a list of command line arguments<\/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":[521,523,524,173,312,525,522],"class_list":["post-8489","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-alphabetical-order","tag-perl-cmp","tag-perl-command-line-arguments","tag-perl-programming","tag-perl-sort-function","tag-perl-sort-function-for-strings","tag-string-comparison"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8489","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=8489"}],"version-history":[{"count":29,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8489\/revisions"}],"predecessor-version":[{"id":8518,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/8489\/revisions\/8518"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=8489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=8489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=8489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}