Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Perl Help


Dystopia's Avatar
Member
0 0

Hey all,

I'm writing a program which is supposed to take numeric input within the range of 0-99 and output it to the actual word that represents the number, as well as the range that the number is in (0-9, 10-19, 20-99). Here's what I have so far:

$ARGV[0] = <STDIN>;
my $Input = $ARGV[0];
print "\n";
print "$Input is in the range of: ";
CASE:
if($Input >= 0 && $Input <=9){
   print "Range of the number is 0-9";
   }
if($Input >= 10 && $Input <= 19){
   print "Range of the number is 10-19";
   }
if($Input >= 20 && $Input <= 99){
   print "Range of the number is 20-99";
   }

I'm thinking that I'll do something like this afterwards:

@Tens = qw(ten twenty thirty fourty fifty sixty seventy eighty ninety);

@ZeroToNine = qw(zero one two three four five six seven eight nine);

@Others = qw(eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen);

Then just call them from the arrays. But I'm not too sure how to go about calling them and thinking that there might be an easier way. I don't have the option to use any pre-built modules (school assignment). Is there anyone that would be able to point me in the right direction? I feel like this is really easy, but I'm just not seeing it. Thanks!


spyware's Avatar
Banned
0 0

The only words you will need are; one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty

Store these in an array/arrays and think about how you can grab a number and get the right word(s) for it.


ghost's Avatar
0 0

… and "zero", since he's doing a greater than or equal to in the condition.

Dystopia wrote:

CASE:
if($Input >= 0 && $Input <=9){
   print "Range of the number is 0-9";
   }
if($Input >= 10 && $Input <= 19){
   print "Range of the number is 10-19";
   }
if($Input >= 20 && $Input <= 99){
   print "Range of the number is 20-99";
   }

You've already broken it down into logical groupings. Use spy's hint at the numbers you need and figure out how to format the twenty, thirty, etc. from those (it's not hard) for the last conditional. It will be easy once you figure out how to handle each number of the last conditional separately.


Dystopia's Avatar
Member
0 0

So, I've still been trying to figure this out. Part of the code below is obviously pseudocode.

if($Input >= 20 && $Input <= 99){  

tensvalue = 1st character of given input
onesvalue = 2nd character of given input

print "The number is " . $tens(tensvalue-1) . (if onesvalue != 0) {$ZeroToNine(onesvalue-1)} "
}
else if ($Input >= 10 && $Input <= 19)
{
print "The number is " . $Others(number - 11)
}
else if ($Input >= 20 && $Input <= 99)
{
print "The number is " . $ZeroToNine(number-1);
}
}

If I were to type as input "10", how would I separate the "1" and "0"? Maybe I can do it another way? I think I've made some progress in the right direction…


ghost's Avatar
0 0
  1. Divide by 10 and round down.
  2. Modulus.

Dystopia's Avatar
Member
0 0

I've finally been able to get the program to work.:D


spyware's Avatar
Banned
0 0

Dystopia wrote: I've finally been able to get the program to work.:D

Cool. Etiquette states you should post your finished code.


Dystopia's Avatar
Member
0 0
my $number;
foreach $number (0..99) {
	my $numbertext = GetNumberText($number);
	my $rangetext = GetRangeText($number);

	print "$number is in the $rangetext range, and its english form is \"$numbertext\"\n";

}
print "Please enter a number, Press ctrl-D to Stop Processing: \n";

while (chomp($number = <>)) {
	my $numbertext = GetNumberText($number);
	my $rangetext = GetRangeText($number);

	print "Your number, $number, is in the $rangetext range, and its english form is \"$numbertext\"\n";
}

sub GetNumberText {
	my ($number) = @_;

	my %numbermap = (
		0 => 'zero',
		1 => 'one',
		2 => 'two',
		3 => 'three',
		4 => 'four',
		5 => 'five',
		6 => 'six',
		7 => 'seven',
		8 => 'eight',
		9 => 'nine',
                10 => 'ten',
                11 => 'eleven',
                12 => 'twelve',
                13 => 'thirteen',
                14 => 'fourteen',
                15 => 'fifteen',
                16 => 'sixteen',
                17 => 'seventeen',
                18 => 'eighteen',
                19 => 'nineteen',
	);

	my %prefixmap = (
		2 => 'twenty',
		3 => 'thirty',
		4 => 'forty',
		5 => 'fifty',
                6 => 'sixty',
                7 => 'seventy',
                8 => 'eighty',
                9 => 'ninety',
	);

	if ($number < 20) {
		return $numbermap{$number};
	}
	elsif ($number < 100) {
		my $mostsignificant = substr($number, 0, 1);
		my $leastsignificant = substr($number, 1, 1);
		my $suffix = " ".$numbermap{$leastsignificant} if $leastsignificant;

		return "$prefixmap{$mostsignificant}$suffix";
	}
	else {
		die "Can't handle numbers >= 100";
	}
}

sub GetRangeText {
	my ($number) = @_;
	if ($number >= 0 && $number < 10) {
		return "0-9";
	}
	elsif ($number >= 10 && $number < 20 ) {
		return "10-19";
	}
	elsif ($number >= 20 && $number < 100 ) {
		return "20-99";
	}
	else {
		die "Number is outside of the valid range: $number";
	}
}