• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Does this work?

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
beefcakegt
Asked:
beefcakegt
  • 2
  • 2
1 Solution
 
maneshrCommented:
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
 
maneshrCommented:
you have posted this Q twice. pl. delete one of them.
0
 
beefcakegtAuthor Commented:
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
 
ozoCommented:
> 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
 
beefcakegtAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now