Follow us on Twitter!
The measure of a mans life is not how well he dies, but how well he lives.
Sunday, April 20, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 27
Guests Online: 25
Members Online: 2

Registered Members: 82843
Newest Member: hx47
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Perl Help

Dystopia
Member



Posts: 22
Location: Localhost
Joined: 15.09.09
Rank:
Moderate
Posted on 13-03-10 23:24
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:

Code

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

Code

@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!
Author

RE: Perl Help

spyware
Member



Posts: 4192
Location: The Netherlands
Joined: 14.04.07
Rank:
God
Warn Level: 90
Posted on 14-03-10 00:06
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.



img507.imageshack.us/img507/3580/spynewsig3il1.png
"The chowner of property." - Zeph
[small]
Widespread intellectual and moral docility may be convenient for leaders in the short term,
but it is suicidal for nations in the long term.
- Carl Sagan
“Since the grid is inescapable, what were the earlier lasers about? Does the corridor have a sense of humor?” - Ebert
[/s
http://bitsofspy.net
Author

RE: Perl Help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 14-03-10 01:17
... and "zero", since he's doing a greater than or equal to in the condition.

Dystopia wrote:
Code

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.


Author

RE: Perl Help

Dystopia
Member



Posts: 22
Location: Localhost
Joined: 15.09.09
Rank:
Moderate
Posted on 15-03-10 04:21
So, I've still been trying to figure this out. Part of the code below is obviously pseudocode.

Code

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...
Author

RE: Perl Help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 15-03-10 04:54
1. Divide by 10 and round down.
2. Modulus.


Author

RE: Perl Help

Dystopia
Member



Posts: 22
Location: Localhost
Joined: 15.09.09
Rank:
Moderate
Posted on 15-03-10 17:13
I've finally been able to get the program to work.Grin
Author

RE: Perl Help

spyware
Member



Posts: 4192
Location: The Netherlands
Joined: 14.04.07
Rank:
God
Warn Level: 90
Posted on 15-03-10 17:19
Dystopia wrote:
I've finally been able to get the program to work.Grin


Cool. Etiquette states you should post your finished code.



img507.imageshack.us/img507/3580/spynewsig3il1.png
"The chowner of property." - Zeph
[small]
Widespread intellectual and moral docility may be convenient for leaders in the short term,
but it is suicidal for nations in the long term.
- Carl Sagan
“Since the grid is inescapable, what were the earlier lasers about? Does the corridor have a sense of humor?” - Ebert
[/s
http://bitsofspy.net
Author

RE: Perl Help

Dystopia
Member



Posts: 22
Location: Localhost
Joined: 15.09.09
Rank:
Moderate
Posted on 15-03-10 17:37
Code

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";
   }
}