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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fheleno
    New Member
    • Nov 2009
    • 4

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

    Hi,
    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

    Fernando
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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:

    Code:
    $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:

    Code:
    my $newstr = lc($str);
    That should work much better for you.

    Regards,

    Jeff

    Comment

    • fheleno
      New Member
      • Nov 2009
      • 4

      #3
      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

      Fernando

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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.

        Regards,

        Jeff

        Comment

        • fheleno
          New Member
          • Nov 2009
          • 4

          #5
          Hi,
          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:
          Code:
          INICIO: if (defined($_ = <STDIN>)) {$t=$_;if($_==0){exit;}}
          print$t."\n";
          
          #my $t1=lc($t);
          
          #$t1=~ s/A-Z/a-z/g;
          
          $t1=~ tr/A-Z/a-z/;
          
                       
          print $t1;
          
          goto INICIO;
          Thank you again and best regards

          Fernando

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            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.
            Code:
            use strict;
            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.

            Comment

            • fheleno
              New Member
              • Nov 2009
              • 4

              #7
              Hi,
              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

              Fernando

              Comment

              Working...