Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Subroutine Question

by Socrates440 (Acolyte)
on Jun 27, 2012 at 03:40 UTC ( [id://978555]=note: print w/replies, xml ) Need Help??


in reply to Re: Subroutine Question
in thread Subroutine Question

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.

Replies are listed 'Best First'.
Re^3: Subroutine Question
by frozenwithjoy (Priest) on Jun 27, 2012 at 04:25 UTC

    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.
        Good to hear that you've got it working. If you look at line 20 of the code I posted, it uses ucfirst to capitalize the first letter and gets rid of the need for the second subroutine.
Re^3: Subroutine Question
by CountZero (Bishop) on Jun 27, 2012 at 06:17 UTC
    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!
        Task::Lingua::Any::Numbers will install all the existing language modules for you. Presently these are are: ID, BASE, NL, NO, PT, TR, EU, DE, BG, JA, AF, SV, IT, PL, CS, ES, FR and EN.

        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
Re^3: Subroutine Question
by mendeepak (Scribe) on Jun 27, 2012 at 06:47 UTC

    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*=*=*

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://978555]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found