Changing from capital letters to small letters using perl

Discussion in 'Perl Misc' started by Venugopal, Nov 5, 2003.

  1. Venugopal

    Venugopal Guest

    Hi,

    Is there an easy way to change all capital letters in a text file
    to small letters using Perl? (Or any other tool!)


    Thanking in advance,
    Venugopal
     
    Venugopal, Nov 5, 2003
    #1
    1. Advertisements

  2. Gunnar Hjalmarsson, Nov 5, 2003
    #2
    1. Advertisements

  3. Yep, perl has a "lower case" function called "lc"

    perldoc -f lc

    A short example:

    #!/usr/bin/perl -w
    use strict;
    undef $/; #enable 'slurp' mode
    open(FILE,'filename.txt') || die $!;
    $_=<FILE>; #read entire file into $_
    close FILE;
    print lc; #print lowercase version (lc defaults to $_)
    __END__

    cheers,

    big (yeah yeah, theres no need to not do it line by line...)
     
    Iain Chalmers, Nov 5, 2003
    #3

  4. which directs you to:

    "If you want to map strings between lower/upper cases, see
    perlfunc/lc and perlfunc/uc"

    out of interest, I did this:

    #!/usr/bin/perl -w
    use strict;
    use Benchmark;

    my $text='aBcDeFgHiJkLmNoPqUsTuVwXyZ';
    my $result;

    timethese(1000000, {
    'lc' => sub {$result=lc($text)},
    'tr' => sub {($result = $text)=~ tr/A-Z/a-z/;},
    });

    and got this:

    Benchmark: timing 1000000 iterations of lc, tr...
    lc: 0 wallclock secs ( 1.67 usr + 0.00 sys = 1.67 CPU)
    @ 598802.40/s (n=1000000)
    tr: 3 wallclock secs ( 2.40 usr + 0.00 sys = 2.40 CPU)
    @ 416666.67/s (n=1000000)


    which makes lc look quite a bit quicker. If I change it so

    my $text='aBcDeFgHiJkLmNoPqUsTuVwXyZ' x 100;

    I get:

    Benchmark: timing 1000000 iterations of lc, tr...
    lc: 64 wallclock secs (46.09 usr + 0.00 sys = 46.09 CPU)
    @ 21696.68/s (n=1000000)
    tr: 78 wallclock secs (55.29 usr + 0.00 sys = 55.29 CPU)
    @ 18086.45/s (n=1000000)

    And, of course, lc knows about locale where tr doesn't...

    big
     
    Iain Chalmers, Nov 5, 2003
    #4
  5. Venugopal

    Roland Mösl Guest

    Is there an easy way to change all capital letters in a text file
    $text =~ y/A-Z/a-z/;
     
    Roland Mösl, Nov 5, 2003
    #5
  6. Maybe. I thought this would be an appropriate task for a one liner,
    and I made this work:

    $ perl -p -e 'tr/A-Z/a-z/' < oldfile.txt > newfile.txt

    but didn't find any way to do it using lc(). (As a W98 user I almost
    never play with one liners...)
     
    Gunnar Hjalmarsson, Nov 5, 2003
    #6
  7. Which of course works only for a small subset of letters even in
    ISO-Latin-1, not to mention other character sets.
    Better to use the proper function lc().

    jue
     
    Jürgen Exner, Nov 5, 2003
    #7
  8. % perl -pe '$_ = lc' old > new
     
    Steve Grazzini, Nov 5, 2003
    #8
  9. Also sprach Jürgen Exner:
    Plus 'use locale', otherwise lc() will just handle those characters in
    the ASCII character set.

    Tassilo
     
    Tassilo v. Parseval, Nov 5, 2003
    #9
  10. Thanks. Stupid mistake (of course): I tried 'lc' instead of '$_ = lc'.
     
    Gunnar Hjalmarsson, Nov 5, 2003
    #10
  11. Hey, didn't the group just have this discussion, a few days back?
    That's clear enough if you're working in one 8-bit locale.

    See the earlier discussion re strange interactions between "use
    locale" and Unicode support, if you go beyond that.
     
    Alan J. Flavell, Nov 5, 2003
    #11
  12. Also sprach Alan J. Flavell:
    Hom come that I somehow assumed that you, Alan, would pop in and join
    this thread? :)
    No, I absolutely wont. Localization stops for me when it involves more
    than eight bits. I am clueless to an almost criminal extent when it
    comes to Unicode. :)

    Which is why I tend to read all your articles on this topic here in this
    group more carefully and in-depth since they always tell me things I
    wasn't in the least aware of before.

    Tassilo
     
    Tassilo v. Parseval, Nov 5, 2003
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.