Solved

Does this work?

Posted on 2000-05-15
5
259 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script 4 97
Need help with bash and/or perl commands on OS X Terminal 9 100
Executing multiple sybase statements in perl dbi 2 88
quoting a comma separated list 20 81
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

16 Experts available now in Live!

Get 1:1 Help Now