Solved

Does this work?

Posted on 2000-05-15
5
258 Views
Last Modified: 2010-03-05
Hey all,

This should be an easy gimme: basically, I want to check if my program works, and I can't at work.  So if one of you guys could run this through and confirm that it works (or not), I'd appreciate it a lot!

Thanks,
Steve

P.S. I'm copying and pasting my program, so hopefully the format doesn't get too screwed up...

#!/usr/bin/perl -w

#description:      This program accepts a line of scalar input from the user.  Depending on what the #            type of scalars are entered, the program will perform 1 of 2 possible actions.

print "Enter a string of characters for analysis: ";
while (<STDIN>) {
   chomp;
   if (/[a-z]/i) {
      count_letters($_);
      count_the($_);
      transform($_);
   } #end if
   else {
      @numlist = split;      #Cuts out extra white space and creates an array containing only our numbers
      my $total = add_numbers(@numlist);
      print "Your total is: $total\n";
   } #end else
} #end while

sub add_numbers {
   my $sum = 0;
   foreach $number(@_) {
      $sum += $number;
   }
   return $sum;
}

sub count_letters {
   my $letterlist =~ s/[^a-z]/ /gi;            #Get rid of anything not a letter
   my @letters = split(/\d/, $letterlist);      #Splitting that single string of words into an array of letters
   $total_letters = @letters;                  #Gives number of elements in array
   print "Quantity of letters: $total_letters\n";
}

sub count_the {
   while (/the/gi) {
      my $count++;
   }
   print "Occurances of the characters "the": $count\n";
}

sub transform {
   while ($_) {
      tr/aeiou/AEIOU/;
      tr/B-DF-HJ-NP-TV-Z/b-df-hj-np-tv-z/;
   }
   print "Vowels upper case, consonants lower: $_\n";
}




0
Comment
Question by:beefcakegt
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
ID: 2811966
the program has some syntax errors.

here is the fixed version.

i executed it from the prompt, but i am not sure what you are expecting from this program. therefore its difficult to say if it is indeed the useable version.


#!/usr/bin/perl

#description: This program accepts a line of scalar input from the user.  Depen
ding on what the
# type of scalars are entered, the program will perform 1 of 2 possible actions
..

print "Enter a string of characters for analysis: ";
while (<STDIN>) {
   chomp;
   if (/[a-z]/i) {
      count_letters($_);
      count_the($_);
      transform($_);
   } #end if
   else {
      @numlist = split; #Cuts out extra white space and creates an array contai
ning only our numbers
      my $total = add_numbers(@numlist);
      print "Your total is: $total\n";
   } #end else
} #end while

sub add_numbers {
   my $sum = 0;
   foreach $number(@_) {
      $sum += $number;
   }
   return $sum;
}

sub count_letters {
   my ($letterlist);
   $letterlist=~ s/[^a-z]/ /gi; #Get rid of anything not a letter
   my @letters = split(/\d/, $letterlist); #Splitting that single string of wor
ds into an array of letters
   $total_letters = @letters; #Gives number of elements in array
   print "Quantity of letters: $total_letters\n";
}

sub count_the {
      my $count;
   while (/the/gi) {
      $count++;
   }
   print "Occurances of the characters \"the:\" $count\n";
}

sub transform {
   while ($_) {
      tr/aeiou/AEIOU/;
      tr/B-DF-HJ-NP-TV-Z/b-df-hj-np-tv-z/;
   }
   print "Vowels upper case, consonants lower: $_\n";
}
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2811969
you have posted this Q twice. pl. delete one of them.
0
 

Author Comment

by:beefcakegt
ID: 2812148
Damn..I was hoping it would be self-documenting..  But here's the assignment:

Basically user enters in a single string of characters.

If the string contains ONLY numbers (and whitespace), add the numbers together.

If the string contains anything other than integers, the program will:
1) count and output the number of letters
2) count and output the number of times "the" appears in the string
3) change all vowels to capital letters, consonants to lower case.  (There HAS to be a better way than the tr/// I have, right?)

Thanks!
Steve
0
 
LVL 84

Expert Comment

by:ozo
ID: 2812976
> If the string contains ONLY numbers (and whitespace)
Assuming that by numbers you mean digits: !/[^\d\s]/
1) tr/A-Za-z// #unless you mean to count each letter separately, or want to include non-english letters
2) @{[/(the)/g]} #see also `perldoc -q "How can I count the number of occurrences of a substring within a string"`
3) It may be clearer to do it with a single tr///, or by first changing all letters to lc, then capitalizing all vowels
0
 

Author Comment

by:beefcakegt
ID: 2814222
Thanks all for the help, especially for catching those stupid syntax errors (\"...*sigh*)!  I managed to tweak my code a bit more before having to turn it in...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now