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

ActivePerl ~s/ / / and ~tr/ / / and lc() not working

P: 4
I am using Activeperl 5.10.

Its replace methods or functions do not seem working.

I need to replace every uppercase letter by its corresponding lowercase.
I used $str=~ s/A-Z/a-z/; $str=lc($str); and $str=~ s/[A-Z]/[a-z]/;

Could you help, please?

thank you

Nov 11 '09 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 2.5K+
P: 3,503
Instead of using the substitution, you want to use translate. Substitue, the way you are using it, looks for a pattern (on the left) and then replaces it with a replacement (on the right).

You want something like this:

Expand|Select|Wrap|Line Numbers
  1. $str=~ tr/A-Z/a-z/;
Also,with the lc() function, I find it better to put the output into a new variable, instead of the one you are reading from. Here is an example:

Expand|Select|Wrap|Line Numbers
  1. my $newstr = lc($str);
That should work much better for you.


Nov 12 '09 #2

P: 4
Hi Jeff,
Thank you for your answer.

I had tried your tips without success.

Probably my perl interpreter is corrupted.

I will tell you.

best regards

Nov 12 '09 #3

Expert Mod 2.5K+
P: 3,503
Do us a favor and post your code, enclosed in code tags, and we will have a look at it and see if it works.


Nov 13 '09 #4

P: 4
Thank you for your help.

I am using Windows7 and also tried another computer with Windows XP
getting the same result, an empty string.

Here is the code I used ( 3 options) for testing purposes:
Expand|Select|Wrap|Line Numbers
  1. INICIO: if (defined($_ = <STDIN>)) {$t=$_;if($_==0){exit;}}
  2. print$t."\n";
  4. #my $t1=lc($t);
  6. #$t1=~ s/A-Z/a-z/g;
  8. $t1=~ tr/A-Z/a-z/;
  11. print $t1;
  13. goto INICIO;
Thank you again and best regards

Nov 13 '09 #5

Expert Mod 100+
P: 589
EVERY Perl script you write should load the warnings and strict pragmas. If you had done so, then they would have informed you that $t1 was never defined.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
Don't use the goto. That's a holdover from perl 4 and nowdays is only used in very rare cases, which you will probably never come across. Instead, you should be using a while loop.

Do not put multiple statements on a single line.

You need to chomp $t

This looks like a homework assignment, so I won't provide the complete solution, but the clues I gave should help to solve the problem.
Nov 13 '09 #6

P: 4
Thank you.
Your remarks helped me to find a serious error:

Using '==' instead of 'eq' stoped the script too earlier.

Thank you again,

Best regards

Nov 13 '09 #7

Post your reply

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