Link to home
Start Free TrialLog in
Avatar of beefcakegt
beefcakegt

asked on

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




ASKER CERTIFIED SOLUTION
Avatar of maneshr
maneshr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maneshr
maneshr

you have posted this Q twice. pl. delete one of them.
Avatar of beefcakegt

ASKER

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
Avatar of ozo
> 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
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...