Socrates440 has asked for the wisdom of the Perl Monks concerning the following question:
#!/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 = (firstnumtoword($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{$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} ; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Subroutine Question
by GrandFather (Saint) on Jun 27, 2012 at 05:02 UTC | |
by Socrates440 (Acolyte) on Jun 27, 2012 at 21:57 UTC | |
by GrandFather (Saint) on Jun 27, 2012 at 23:56 UTC | |
|
Re: Subroutine Question
by Anonymous Monk on Jun 27, 2012 at 03:18 UTC | |
|
Re: Subroutine Question
by muba (Priest) on Jun 27, 2012 at 04:50 UTC | |
|
Re: Subroutine Question
by frozenwithjoy (Priest) on Jun 27, 2012 at 03:17 UTC | |
by Socrates440 (Acolyte) on Jun 27, 2012 at 03:40 UTC | |
by frozenwithjoy (Priest) on Jun 27, 2012 at 04:25 UTC | |
by Socrates440 (Acolyte) on Jun 27, 2012 at 22:04 UTC | |
by frozenwithjoy (Priest) on Jun 27, 2012 at 22:52 UTC | |
by CountZero (Bishop) on Jun 27, 2012 at 06:17 UTC | |
by frozenwithjoy (Priest) on Jun 27, 2012 at 06:20 UTC | |
by CountZero (Bishop) on Jun 27, 2012 at 06:27 UTC | |
by mendeepak (Scribe) on Jun 27, 2012 at 06:47 UTC |