Solved

Does this work?

Posted on 2000-05-15
5
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

726 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