koolgirl has asked for the wisdom of the Perl Monks concerning the following question:
Once again, I am very new to Perl, so my question is quite basic, perhaps even obvious, but I have spent several hours reading perldoc and am still stumped, so, here goes:
I've just began creating sub routines in exercises, and I'm having a big problem. I'm supposed to write a routine, that takes a number from 1 - 9, and returns the english name of that number, i.e. 1 - "one" , etc. So the program prompts user for number, then sub routine is called. However, it is seriously going wrong :\, this is the code:
#usr/bin/perl use strict; use warnings; my $input; print "Enter a number\n"; chomp($input = <STDIN>); print &number_return ($input); ##sub_routines################################ sub number_return { my $answer = shift(@_); my @numbers = (1,2,3,4,5,6,7,8,9); my @words = ("one","two","three","four","five","six","seven","eight"," +nine"); my $i = 0; my $sum; while ($answer <= 9) { if ($answer eq $numbers[$i]) { $answer = $words[$i]; $i++; $answer; } else { #print $answer; } # end if } # end while } # end sub
First of all, the "#print $answer" is commented out, because I can't currently get it to enter the first part, so it then becomes a problem. Secondly, I have read chp 8 of the llama book (1st ed.) several times, and I've also read perlman:perlsub several times as well. I got many hints from the perlsub page, and made many changes which did help, however, I really don't quite understand or even suspect where the problem is, so it's hard for me to get clues unless I see code examples, which are quite hard to find on things this basic aside from the llama book, and sometimes Randal uses so many fancy short cuts, he eludes me ( please don't mis-understand, all hail Randal..just commenting on my inability, not his ).
So, in the output, I get several errors, one, when the "else" is not commented out, it reprints the number I typed like 5 million times, second, when it is, it does nothing, I type in a number, and it just sits there. I've read alot about passing parameters, and I'm pretty sure, I did that correctly..? I also tried to use "return" and that ended the routine to soon, so, I stopped that, I've been working on this darn thing since this morning, and although I've made quite a bit of progress since, I just can't think of what else to read, cus I'm not sure where the problem is..?
Anyway, sorry Monks, I know this is very trivial compared to most of the problems here, but I just need a point in the right direction, not someone to do it, just a bit of a push, I guess. Also, I know it isn't the most efficient type of coding, and probably a useless function, but the point is to learn how to write a useful subroutine and I'm entirely frustrated at this point.
Thanks Monks, :) UPDATE!!::OK, Monks. This was a huge lesson for me as a rookie. So, I took into consideration all of the replies, namely, about using a hash (jwkrahn), and asking myself the questions Perlbotics provided,( however, each reply brought a new insight ), anyway, so I started again, and again became so frustrated, I thought I might SCREAM! Then, all of a sudden, as I stared at the comp cock-eyed, I saw it:
#!usr/bin/perl my %numbers = qw( 0 zero 1 one 2 two 3 three 4 four 5 five 6 six 7 seven 8 eight 9 nine ); my $input; my $key; print "Enter your number\n"; chomp ($input = <STDIN>); print $numbers{$input} . "\n";
Yes, three lines of code...I had almost destroyed my computer, over something I easily did in about 2 minutes. My thinking was way too uptight. I was so focused on writing code, I barely thought about the problem and it's simplicity. My approach was wrong, and when I really thought about it ( thanks Perlbotics ;) I saw my structure was way off. So, lesson learned, and in front of the entire Perl community, nonetheless. :) So, as I said, I have learned to slow down, think it through, and see the PROBLEM before the code. I thank you PerlMonks for being there through this epiphany in my young Perl life :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sub Routine Malfunction
by Perlbotics (Archbishop) on Aug 28, 2008 at 23:59 UTC | |
|
Re: Sub Routine Malfunction
by jwkrahn (Abbot) on Aug 28, 2008 at 23:52 UTC | |
|
Re: Sub Routine Malfunction
by betterworld (Curate) on Aug 28, 2008 at 23:41 UTC | |
|
Re: Sub Routine Malfunction
by ikegami (Patriarch) on Aug 28, 2008 at 23:47 UTC | |
|
Re: Sub Routine Malfunction
by oko1 (Deacon) on Aug 29, 2008 at 03:20 UTC | |
by koolgirl (Hermit) on Aug 29, 2008 at 04:37 UTC | |
|
Re: Sub Routine Malfunction
by llancet (Friar) on Aug 29, 2008 at 08:49 UTC | |
by apl (Monsignor) on Aug 29, 2008 at 11:56 UTC | |
by koolgirl (Hermit) on Aug 29, 2008 at 12:16 UTC | |
|
Re: Sub Routine Malfunction
by broomduster (Priest) on Aug 28, 2008 at 23:54 UTC | |
|
Re: Sub Routine Malfunction
by zentara (Cardinal) on Aug 29, 2008 at 15:03 UTC | |
|
Re: Sub Routine Malfunction
by FunkyMonk (Bishop) on Aug 29, 2008 at 21:57 UTC | |
|
Re: Sub Routine Malfunction
by gone2015 (Deacon) on Aug 29, 2008 at 15:53 UTC |