in reply to Subroutine Question

You need to change return $nums{$userin2}; to return $nums{$_}; in both subroutines (but you only need one sub).

Replies are listed 'Best First'.
Re^2: Subroutine Question
by Socrates440 (Acolyte) on Jun 27, 2012 at 03:40 UTC
    I just made the correction recommended by frozenwithjoy and now my program is returning " plus equals " I am more confused than ever. Before, my return variable shared the same name as the variable that I inputed into the subroutine with shift. Then, I noticed that the variable name associated with shift seemed to be irelevant so I changed it to $_ and everything worked except for sum. Just now, for congruency's sake, I changed the return variable to $_ and now nothing works. By what logic is this hapening? This is a fascinating bug to me.

      Hmmm. It works great for me. Are you certain that you made the correct changes and no others? Before my changes, the output is:

      This program will ask you to type in two numbers and then add those nu +mbers together. It will then display the problem and the answer in word form. Type in a single digit number between one and four, or type done to ex +it. 2 Type in a single digit number between one and five 4 Use of uninitialized value $sum in concatenation (.) or string at ./su +m2.pl line 23, <STDIN> line 2. Four plus four equals Type in a single digit number between one and four, or type done to ex +it. done

      Notice how I input 2 and 4, but it only saw 4 and 4. That is because you were using </c>$userin2</c> in your return. After the changes, the output is:

      This program will ask you to type in two numbers and then add those nu +mbers together. It will then display the problem and the answer in word form. Type in a single digit number between one and four, or type done to ex +it. 2 Type in a single digit number between one and five 4 Two plus four equals six Type in a single digit number between one and four, or type done to ex +it. done

      Here is the altered script. The only changes are on lines 20 and 32 (also 2nd subroutine is commented out).

      #!/usr/bin/perl -w use strict ; my (@a, $userin, $userin2, $sum) ; @a = (1 .. 9) ; print "This program will ask you to type in two numbers and then add t +hose numbers together.\nIt will then display the problem and the answ +er in word form.\n" ; $userin = 0 ; while ($userin ne "done") { print "Type in a single digit number between one and four, or type + done to exit.\n" ; chomp ($userin = <STDIN>) ; if ($userin eq "done") { last ; } print "Type in a single digit number between one and five\n" ; chomp ($userin2 = <STDIN>) ; if (($userin =~ /^[^1-4]$/) || ($userin2 =~ /^[^1-5]$/)) { print "I do not understand\n" ; } else { $sum = ($userin + $userin2) ; $userin = ucfirst (numtoword($userin)) ; #was: $userin = (f +irstnumtoword($userin)) ; $userin2 = (numtoword($userin2)) ; $sum = (numtoword($sum)) ; print "$userin plus $userin2 equals $sum\n" ; } } sub numtoword { $_ = shift ; my (%nums) ; %nums = ("1"=>"one", "2"=>"two", "3"=>"three", "4"=>"four", "5"=>" +five", "6"=>"six", "7"=>"seven", "8"=>"eight", "9"=>"nine") ; return $nums{$_} ; #was: return $nums{$userin2} ; } # sub firstnumtoword { # $_ = shift ; # my (%nums2) ; # %nums2 = ("1"=>"One", "2"=>"Two", "3"=>"Three", "4"=>"Four", "5" +=>"Five", "6"=>"Six", "7"=>"Seven", "8"=>"Eight", "9"=>"Nine") ; # return $nums2{$userin2} ; # }

      Also, it would be more readable to write your subroutine like this:

      sub numtoword { my $digit = shift; my %nums; %nums = ( "1"=>"one", "2"=>"two", "3"=>"three", "4"=>"four", "5"=> +"five", "6"=>"six", "7"=>"seven", "8"=>"eight", "9"=>"nine" ); return $nums{$digit};
        I found the bug :) I had my varriables mixed up. The reason that I created a second subroutine was so that I could capitalize the first letter of the first number. It was a part of the exercize.
      Could it be that perhaps $sum was bigger than 9? If so, there is nothing in the hash to find and you get an undef value returned.

      Have a look at Lingua::Any::Numbers. It can convert numbers to strings.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
        And multiple language support?! That's hot!

      i tried what frozenwithjoy has suggested to you and it worked superbly well for me. can you post the code you have made to the changes to...

      *=*=*dEEPAk*=*=*