By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,161 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,161 IT Pros & Developers. It's quick & easy.

problem with uppercase and lowercase:

P: 24
hi guys,i have this problem.first let me show you the so far code

Expand|Select|Wrap|Line Numbers
  1. #!/user/bin/perl
  2. while ($a = <STDIN>)
  3. {
  4. if ($a =~ tr/A-Z/a-z/)
  5. {
  6.     print "$a\n";
  7. }
  8. if ($a =~ tr/a-z/A-Z/)
  9. {
  10.     print "the uppercase is $a\n";
  11. }
  12.  
  13. elsif ($a =~ tr/[A-Z]/[a-z]/)
  14. {
  15.     $b=$a;
  16.         $b =~ tr/A-Z/a-z/c;
  17.     print "$b\n";
  18. }
  19. }
  20.  
i got the conversion of uppercase to lowercase and vice verse,
but,the problem is i want the first letter of the output in uppercase and remaining in lowercase.
Sep 11 '07 #1
Share this Question
Share on Google+
9 Replies


KevinADC
Expert 2.5K+
P: 4,059
You need to get familiar with all of perls builtin functions:

http://perldoc.perl.org/index-functions.html

You will find a function called 'ucfirst' in the above list of functions.
Sep 11 '07 #2

P: 2
Hi

for getting the first letter in upper case or lowercase,u have to use ^

like this

$x=~s/^[A-Z]/[a-z]/;

hi guys,i have this problem.first let me show you the so far code

Expand|Select|Wrap|Line Numbers
  1. #!/user/bin/perl
  2. while ($a = <STDIN>)
  3. {
  4. if ($a =~ tr/A-Z/a-z/)
  5. {
  6.     print "$a\n";
  7. }
  8. if ($a =~ tr/a-z/A-Z/)
  9. {
  10.     print "the uppercase is $a\n";
  11. }
  12.  
  13. elsif ($a =~ tr/[A-Z]/[a-z]/)
  14. {
  15.     $b=$a;
  16.         $b =~ tr/A-Z/a-z/c;
  17.     print "$b\n";
  18. }
  19. }
  20.  
i got the conversion of uppercase to lowercase and vice verse,
but,the problem is i want the first letter of the output in uppercase and remaining in lowercase.
Sep 13 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
Hi

for getting the first letter in upper case or lowercase,u have to use ^

like this

$x=~s/^[A-Z]/[a-z]/;
That is not true, but it is an option that might apply to a particular situation. (Your example will convert the very first letter of a string into lower-case). But that may or may not be what humaid is trying to do. But since humaid has not replied to the question we may never know.
Sep 13 '07 #4

P: 24
That is not true, but it is an option that might apply to a particular situation. (Your example will convert the very first letter of a string into lower-case). But that may or may not be what humaid is trying to do. But since humaid has not replied to the question we may never know.
hi guys,the problem is,suppose i give the input as,

hi this is humaid
i want the output as:Hi This Is Humaid
this is what iam trying.
Sep 14 '07 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
hi guys,the problem is,suppose i give the input as,

hi this is humaid
i want the output as:Hi This Is Humaid
this is what iam trying.
The following does what you want:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $phrase = "this is nobody.";
  5.  
  6. print("Unmodified phrase:  $phrase\n");
  7.  
  8. my @split_phrase = split(/\s/, $phrase);
  9.  
  10. my @new_phrase;
  11. my $var1;
  12.  
  13.  
  14. foreach my $word (@split_phrase)
  15. {
  16.     chomp($word);
  17.     $var1 = ucfirst $word;
  18.     push(@new_phrase, $var1);
  19. }
  20.  
  21. print("@new_phrase");
  22.  
Regards,

Jeff
Sep 14 '07 #6

KevinADC
Expert 2.5K+
P: 4,059
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my $phrase = "this is nobody.";
  4. $phrase =~ s/(\w+)/ucfirst($1)/eg;
  5. print $phrase;
  6.  
Sep 14 '07 #7

numberwhun
Expert Mod 2.5K+
P: 3,503
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my $phrase = "this is nobody.";
  4. $phrase =~ s/(\w+)/ucfirst($1)/eg;
  5. print $phrase;
  6.  
Sure, one-up me. I see how you are! ;-|)

See, this is Perl. Just another example that TIMTOWTDI.

Regards,

Jeff
Sep 14 '07 #8

KevinADC
Expert 2.5K+
P: 4,059
Sure, one-up me. I see how you are! ;-|)

See, this is Perl. Just another example that TIMTOWTDI.

Regards,

Jeff
naaaaa.....not one-up-manship. I wasn't going to post code but since code has been posted I figured I'd throw my 2 cents in. humaid can decide what is best for his situation.

BTW.... have you "seen" Miller lately?
Sep 14 '07 #9

numberwhun
Expert Mod 2.5K+
P: 3,503
naaaaa.....not one-up-manship. I wasn't going to post code but since code has been posted I figured I'd throw my 2 cents in. humaid can decide what is best for his situation.

BTW.... have you "seen" Miller lately?
No worries, I was just bustin. Nice way of doing it. I like learning the shortcuts.

As a matter of fact, no, I have not seen our other Moderator around at all and was beginning to wonder if he has dropped off the face of the {forum|earth}. Hopefully he will apparate soon to say hello.

Regards,

Jeff
Sep 14 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.