in reply to Re^2: Subroutine Question
in thread Subroutine Question
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};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Subroutine Question
by Socrates440 (Acolyte) on Jun 27, 2012 at 22:04 UTC | |
by frozenwithjoy (Priest) on Jun 27, 2012 at 22:52 UTC |