6 Replies - 1247 Views - Last Post: 16 March 2010 - 08:31 AM

#1 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Perl Subroutine program

Posted 15 March 2010 - 10:29 AM

Hey,
So i'm pretty new to perl and i'm working on a project a friend gave me working with subroutines. I am currently pretty stuck, I have to create a program that uses subroutines to output text based on what you type in. Its supposed take input from the user in the form of a number from 1-99 and use subroutines to print the word of the number they input. so if a user enters 55 it will output fifty five. Here is what i have started.
[code] @Ones = (One, Two, Three, Four, Five Six, Seven, Eight, Nine);
@Teens = (Ten, Eleven, Tweleve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen);
@Tens = (Twenty, Thrity, Fourty, Fifty, Sixty, Seventy, Eighty, Ninety);

print "Enter a number between 1 and 99\n";
$Input = <STDIN>;

sub Ones { [code]

any help would be great im also gonna attach the lab im working through, i just dont know where to go next with this. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Perl Subroutine program

#2 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: Perl Subroutine program

Posted 15 March 2010 - 05:26 PM

Hey,
I am working on a project that takes numbers as input between 0 and 99 and outputs the word, so like it says enter a number, you enter 5, it outputs five... I have got stuck on getting output so here is the code I have gotten so far and any help would be wonderful, thanks
print "Enter a number between 1 and 99\n";
$Input = <STDIN>;

$Input = $ARGV[0];

sub Ones {
	@Ones = qw(Zero One Two Three Four Five Six Seven Eight Nine);
	return $Ones[$_[0]];
}

sub Teens {
	@Teens = qw(Ten Eleven Tweleve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen);
	return $Teens[$ARGV[0] - 10];
}

sub Tens {
	@Tens = qw(Twenty Thirty Fourty Fifty Sixty Seventy Eighty Ninty);
	return $Tens[$_[0]-2];
}

if ($Input <= 9)
{
	print $Ones;
}

Was This Post Helpful? 0
  • +
  • -

#3 Pwn   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Re: Perl Subroutine program

Posted 15 March 2010 - 05:40 PM

I don't see where you're putting the return statements into anything in your main program, also, I don't see where you're calling the subroutines in your program at all. Your sub, if it returns something, has to have somewhere to put the value, be it a variable or a placed in a string build. But, also, and this ties into what I just said, you have to call the sub in order for it to do anything. Hope that helps.

What I see in your program is this:
if x<9 {print $Ones}

This is printing a variable (declared in this very statement) called Ones, which, by default, is null.
Then you have a sub:
sub Ones
{yadda yadda yadda]

To call this sub, I believe you need to have a line that says Ones() with optional parameters in the perenthesis. The parameters you put in the perenthesis are what will be in the (local to this sub) variable $_0

Somebody correct me if I'm wrong, I'm still learning this myself.

This post has been edited by Pwn: 15 March 2010 - 05:48 PM

Was This Post Helpful? 1
  • +
  • -

#4 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: Perl Subroutine program

Posted 15 March 2010 - 06:04 PM

.

This post has been edited by stsmiley: 15 March 2010 - 06:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 stsmiley   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-November 09

Re: Perl Subroutine program

Posted 15 March 2010 - 06:12 PM

Ok so everything works for the numbers 1-19 and i can get 20,30,40,50,60,70,80, and 90 but i cant get numbers like 23 or 55, numbers in between those... heres my whole code as it stands now and this is all i have left, if anyone has an idea to throw out that would be greatly appreciated.
$Input = $ARGV[0];

print "Enter a number between 1 and 99\n";
$Input = <STDIN>;


sub Ones {
	@Ones = qw(Zero One Two Three Four Five Six Seven Eight Nine);
	return $Ones[$_[0]];
}

sub Teens {
	@Teens = qw(Ten Eleven Tweleve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen);
	return $Teens[$_[0]];
}

sub Tens {
	@Tens = qw(Twenty Thirty Fourty Fifty Sixty Seventy Eighty Ninty);
	return $Tens[$_[0]-2];
}

$Calculate1 = $Input % 10;
$Calculate2 = $Input / 10;

if ($Input <= 9)
{
	Ones ();
	print Ones($Calculate1);
}

if (($Input >= 10) and ($Input <= 19))
{
	Teens ();
	print Teens($Calculate1);
}

if (($Input >= 20) and ($Input <= 99))
{
	Tens ();
	print Tens($Calculate2);
}e

This post has been edited by stsmiley: 15 March 2010 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Perl Subroutine program

Posted 16 March 2010 - 05:39 AM

View PostPwn, on 16 March 2010 - 12:40 AM, said:

What I see in your program is this:
if x<9 {print $Ones}

This is printing a variable (declared in this very statement) called Ones, which, by default, is null.


...which is why you should always start your Perl code with
use strict;
use warnings;


In this case, if the OP had enabled "strict", that line would have failed to compile with the error
Global symbol "$Ones" requires explicit package name
because it had not been properly declared.

View Poststsmiley, on 16 March 2010 - 01:12 AM, said:

Ok so everything works for the numbers 1-19 and i can get 20,30,40,50,60,70,80, and 90 but i cant get numbers like 23 or 55, numbers in between those... heres my whole code as it stands now and this is all i have left, if anyone has an idea to throw out that would be greatly appreciated.


I've done a quick pass over your posted code, declared the variables correctly (so that it will run with strict/warnings), changed the variable names (normal style in perl is to use all_lowercase names), and added comments pointing you towards the answer to your question and to things I think could have been done better (I'm assuming you want to figure things out for yourself and are just looking for guidance; if you want explicit answers instead of hints, just ask). Here's the result:
#!/usr/bin/perl

use strict;
use warnings;

# What is the purpose of this assignment?  This value of $input is never
# used.
my $input = $ARGV[0];

print "Enter a number between 1 and 99\n";
$input = <STDIN>;


# Normal Perl style is to put your main block of code first and define subs
# after it, so that a reader can immediately see an overview of what's going on
# instead of being thrown into all the small details without a clear picture of
# how they're going to fit together.

sub ones {
        my @ones = qw(Zero One Two Three Four Five Six Seven Eight Nine);
        return $ones[$_[0]];
}

sub teens {
        my @teens = qw(Ten Eleven Tweleve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen);
        return $teens[$_[0]];
}

sub tens {
        my @tens = qw(Twenty Thirty Fourty Fifty Sixty Seventy Eighty Ninty);
        # Ugh...  That $_[0]-2 is ugly!  Maybe you could eliminate the need
        # for the -2 if you inserted some dummy values at the beginning of
        # the array...
        return $tens[$_[0]-2];
}

my $calculate1 = $input % 10;
my $calculate2 = $input / 10;

# Since no more than one of these "if" statements can be true, consider making
# use of the "elsif" and "else" keywords
if ($input <= 9)
{
        # You don't need to call the sub twice.  The non-printed call just
        # returns a value that gets thrown away without doing anything, so
        # why bother?
        # 
        # I commented them out myself instead of leaving that for you to do
        # because, in addition to not doing anything, they also cause
        # "uninitialized value" warnings when you try to use the passed value
        # as an array index in the subs, even though no value was passed
        #ones ();

        # Don't forget to print a "\n" at the end of your output so that the
        # cursor will be moved onto the next line before your program exits.
        print ones($calculate1);
}

if (($input >= 10) and ($input <= 19))
{
        #teens ();
        print teens($calculate1);
}

if (($input >= 20) and ($input <= 99))
{
        #tens ();

        # Now, for the actual question you asked...
        #
        # You're already printing out the tens part of the number.  All you
        # need to do to complete it is to print out the ones part after the
        # tens - unless the ones part is 0.
        #
        # Another option would be to modify the tens() sub so that it takes
        # two parameters, the tens digit and the ones digit, and then call
        # ones() from tens() if needed.
        print tens($calculate2);
}


Was This Post Helpful? 0
  • +
  • -

#7 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Perl Subroutine program

Posted 16 March 2010 - 08:31 AM

Are you allowed to use CPAN modules? Are you sure the question requires you to write a subroutine for every number, and doesn't just want you to write a generic sub to convert integers into english? This seems like more of a dispatch table problem to me:
my %numbers = {
 1 => 'one',
 2 => 'two',
}
sub number_to_english {
 return $numbers[$_[0]];
}
print "Enter a number between 1 and 99";
my $input = <STDIN>;
print number_to_english($input);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1