{"id":5157,"date":"2014-08-14T19:31:11","date_gmt":"2014-08-14T19:31:11","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=5157"},"modified":"2014-08-14T19:31:11","modified_gmt":"2014-08-14T19:31:11","slug":"perl-programming-the-user-input-array","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-programming-the-user-input-array\/","title":{"rendered":"Perl programming:  the user input array"},"content":{"rendered":"<h1>The tutor notices that, given it&#8217;s Aug 14, we need a little progress in our summer project of Perl programming.\u00a0 This rarely comes up in tutoring, but it&#8217;s a labour of love&#8230;.<\/h1>\n<p>&nbsp;<br \/>\nBack on <a href=\"?p=4589\">June 22,<\/a> I broached the idea of getting started with Perl.  After all, I argued, academic pursuits can continue through the summer, the motivation being interest rather than preparing for an exam.<\/p>\n<p>For those of you who actually took up the challenge and got started, you likely followed my blog through the summer.  Not every article has been about Perl, but a good few have.  The time has come for more.<\/p>\n<p>Today&#8217;s article plugs an obvious hole in our knowledge base so far:  getting user input.  After all, it&#8217;s hard to interact with a program if you can&#8217;t give it different values to see how the output changes.<\/p>\n<p>Perl has an array called @ARGV that is dedicated to storing inputs from the command line.  (See my article on arrays <a href=\"?p=5096\">here<\/a>.)  The following program serves as an example:<\/p>\n<p><span style=\"font-family:monospace;color:brown\"><br \/>\n#!\/usr\/bin\/perl<br \/>\n$name=$ARGV[0];<br \/>\n$firstnum=$ARGV[1];<br \/>\n$secondnum=$ARGV[2];<br \/>\n$ans=$firstnum + $secondnum;<br \/>\nprint &#8220;\\n\\nHello, $name.&#8221;;<br \/>\nprint &#8221; You entered $firstnum and $secondnum.&#8221;;<br \/>\nprint &#8221; Their sum is $ans\\n\\n.&#8221;<\/span><\/p>\n<p>The activity of this code is likely not mysterious.  It is given the input array @ARGV with three values in it.  $ARGV[0] is expected to contain your name; $ARGV[1] and $ARGV[2] should each contain a number.  The program fetches your name from $ARGV[0], then stores it in the variable $name. Next, the program fetches the numbers from $ARGV[1] and $ARGV[2], stores them in $firstnumber and $secondnumber, adds the two, then stores the sum in $ans.  On the command line, it gives you a friendly greeting by the name you gave, afterwards reminding you the numbers you gave and telling you their sum.  Where you see \\n, the terminal will start a new line.  That&#8217;s just done to make space on the screen for the output.<\/p>\n<p>At this point, the serious reader might have four questions in mind:<\/p>\n<ol>\n<li>  How does your name get into $ARGV[0]?<\/li>\n<p><\/p>\n<li>  How does the first number get into $ARGV[1]?<\/li>\n<p><\/p>\n<li>  How does the second number get into $ARGV[2]?<\/li>\n<p><\/p>\n<li>  How does @ARGV get handed to the program as input?<\/li>\n<\/ol>\n<p>The answer is that you enter those inputs, in order, after the program name.  Let&#8217;s imagine the program above is called <span style=\"font-family:monospace\">lucy.txt<\/span>,and that your own name is Edward.  Furthermore, you are just now wondering what the sum of 329 and 1982 is.  Without a pencil or calculator handy, you&#8217;d like to call on <span style=\"font-family:monospace\">lucy.txt<\/span> for the answer.   First, you will go into the terminal (see my articles <a href=\"?p=4658\">here, <\/a><a href=\"?p=4699\">here,<\/a> and\/or <a href=\"?p=4732\">here<\/a>, depending on your operating system).  Next, you will place yourself in the directory where <span style=\"font-family:monospace\">lucy.txt<\/span> resides.  Finally, you will call her and give her the three inputs by entering the following line:<\/p>\n<p><span style=\"font-family:monospace\">perl lucy.txt Edward 329 1982<\/span><\/p>\n<p>If all goes well, Lucy will answer with<\/p>\n<p><span style=\"font-family:monospace\">Hello, Edward.  You entered 329 and 1982.  Their sum is 2311.<\/span><\/p>\n<p>The very first line of <span style=\"font-family:monospace\">lucy.txt<\/span>, specifically<\/p>\n<p><span style=\"font-family:monospace;color:brown\">#!\/usr\/bin\/perl<\/span><\/p>\n<p>is called the shebang line.  (You can read more about it <a href=\"?p=4832\">here.<\/a>)  It is needed in non-Windows environments.  While it&#8217;s not necessary in Windows,  it doesn&#8217;t seem to hurt if it&#8217;s there.<\/p>\n<p>Good luck with this!<\/p>\n<p><em>Source<\/em>:  <a href=\"http:\/\/www.sthomas.net\/roberts-perl-tutorial.htm\">Robert Pepper&#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>The tutor notices that, given it&#8217;s Aug 14, we need a little progress in our summer project of Perl programming.\u00a0 This rarely comes up in tutoring, but it&#8217;s a labour of love&#8230;. &nbsp; Back on June 22, I broached the &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-programming-the-user-input-array\/\"> <span class=\"screen-reader-text\">Perl programming:  the user input array<\/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":[197,196,173,198],"class_list":["post-5157","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-argv","tag-perl-input-array","tag-perl-programming","tag-perl-user-input"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/5157","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=5157"}],"version-history":[{"count":52,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/5157\/revisions"}],"predecessor-version":[{"id":5209,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/5157\/revisions\/5209"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=5157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=5157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=5157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}