Hi Monks :),

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 :)


In reply to Sub Routine Malfunction by koolgirl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.