Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Implement uclast with a regex

by Anonymous Monk
on Mar 10, 2004 at 08:21 UTC ( [id://335384]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, conferes

I have a question!
I have a word eg "ACta", but I want "actA". (All char lc, but the last char uc)

s/(\w{3})(\w)/lc$1 uc$2/e ????

What is the best (easiest) way do this?
Thanks!

Edited by Chady -- re-formated, and retitled from regexp

Replies are listed 'Best First'.
Re: Implement uclast with a regex
by Juerd (Abbot) on Mar 10, 2004 at 08:32 UTC

    What is the best (easiest) way do this?

    Easiest would be using reverse twice:

    reverse ucfirst lc reverse "ACta"

    Try not to hurt our brains with hard to read regexes :)

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: Implement uclast with a regex
by Koosemose (Pilgrim) on Mar 10, 2004 at 08:37 UTC

    Not neccessarily the best way to do it, but it is pretty simple:

    scalar reverse ucfirst lc reverse 'ACta';

    note: scalar is only neccessary if you use this in a list context, such as print, but I left it in, just in case.

    Just Another Perl Alchemist
Re: Implement uclast with a regex
by dada (Chaplain) on Mar 10, 2004 at 09:18 UTC
    yet another way, which should be quickier than the double reverse:
    my $string = "ACta"; $string = lc(substr($string, 0, length($string)-1)) . uc(substr($string, -1, 1));
    cheers,
    Aldo

    King of Laziness, Wizard of Impatience, Lord of Hubris

      Well, I get it to almost the same speed as the double reverse, but only after some modification:
      #!/usr/bin/perl use strict; use warnings; use Benchmark qw /cmpthese/; chomp (our @strings = <DATA>); our (@a, @b, @c, @d); cmpthese -1 => { juerd => '@a = map {scalar reverse ucfirst lc reverse} @strings', davido => '@b = map {my $t = $_; $t =~ s/^(.*)(.)$/\L$1\U$2/; $t} +@strings', dada1 => '@c = map {lc (substr ($_, 0, length ($string) - 1)) . uc (substr ($_, -1, 1))} @strings', dada2 => '@d = map {lc (substr ($_, 0, -1)) . uc (substr ($_, -1, 1))} @strings', }; die 1 unless "@a" eq "@b"; die 2 unless "@a" eq "@c"; die 3 unless "@a" eq "@d"; __DATA__ ACta abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnop +qrstuvwxyz I have a question! yet another way, which should be quickier than the double reverse: Rate davido dada1 dada2 juerd davido 21081/s -- -68% -70% -70% dada1 65511/s 211% -- -6% -7% dada2 69592/s 230% 6% -- -1% juerd 70275/s 233% 7% 1% --

      Abigail

      should be quickier than the double reverse

      Why? Have you benchmarked that?

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

        no I didn't, it was just a guess :-) and in fact, it seems the double reverse solution is faster for small strings, while the substr solution wins in the long run. anyway, here you get full benchmark:
        use strict; use List::Util qw(shuffle); use Benchmark qw(:all); my $string = "ACta"; print "*** ", length($string), " characters\n"; cmpthese(1_000_000, { uclast_substr => sub { uclast_substr($string) }, uclast_2reverse => sub { uclast_2reverse($string) }, }); $string = join ("", shuffle( 'A'..'Z', 'a'..'z' )); print "*** ", length($string), " characters\n"; cmpthese(1_000_000, { uclast_substr => sub { uclast_substr($string) }, uclast_2reverse => sub { uclast_2reverse($string) }, }); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); print "*** ", length($string), " characters\n"; cmpthese(1_000_000, { uclast_substr => sub { uclast_substr($string) }, uclast_2reverse => sub { uclast_2reverse($string) }, }); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); $string .= join ("", shuffle( 'A'..'Z', 'a'..'z' )); print "*** ", length($string), " characters\n"; cmpthese(1_000_000, { uclast_substr => sub { uclast_substr($string) }, uclast_2reverse => sub { uclast_2reverse($string) }, }); sub uclast_substr { my($string) = @_; $string = lc(substr($string, 0, length($string)-1)) . uc(substr($string, -1, 1)); return $string; } sub uclast_2reverse { my($string) = @_; $string = reverse ucfirst lc reverse $string; return $string; }
        and here the result:
        *** 4 characters
                            Rate   uclast_substr uclast_2reverse
        uclast_substr   566572/s              --            -15%
        uclast_2reverse 666223/s             18%              --
        *** 52 characters
                            Rate   uclast_substr uclast_2reverse
        uclast_substr   470588/s              --             -6%
        uclast_2reverse 500000/s              6%              --
        *** 208 characters
                            Rate uclast_2reverse   uclast_substr
        uclast_2reverse 249938/s              --             -9%
        uclast_substr   273523/s              9%              --
        *** 676 characters
                            Rate uclast_2reverse   uclast_substr
        uclast_2reverse 102239/s              --            -19%
        uclast_substr   126231/s             23%              --
        
        cheers,
        Aldo

        King of Laziness, Wizard of Impatience, Lord of Hubris

        use Benchmark; my $tests = [qw(these are a few test to try andaverylongteststring acoupleoftimesover xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +xxxxxxxxxxxxxxxxxxxxxxxxxx zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzz)]; timethese (100000, { NoOpLoop => sub { for my $test (@$tests) { # NO Op Loop } }, regex1 => sub { for my $test (@$tests) { $test=~s/^(.*)(.)$/\L$1\U$2/; } }, substr => sub { for my $test (@$tests) { $test= lc(substr($test, 0, length($test)-1)) . uc(substr($test, -1, 1)); } }, reverse=> sub { for my $test (@$tests) { $test=reverse ucfirst lc reverse $test; } } });
        and the winner is :-
        Benchmark: timing 100000 iterations of NoOpLoop, regex1, reverse, subs +tr... NoOpLoop: 2 wallclock secs ( 1.86 usr + 0.00 sys = 1.86 CPU) @ 53763.44/s (n=100000) regex1: 82 wallclock secs (30.31 usr + 0.01 sys = 30.32 CPU) @ 3298.15/s (n=100000) reverse: 8 wallclock secs ( 7.40 usr + 0.00 sys = 7.40 CPU) @ 13513.51/s (n=100000) substr: 8 wallclock secs ( 8.51 usr + 0.01 sys = 8.52 CPU) @ 11737.09/s (n=100000)
        Interesting: double reverse seamed the best but as always test on real data!!

        Hope it helps
        UnderMine

Re: Implement uclast with a regex
by davido (Cardinal) on Mar 10, 2004 at 08:37 UTC
    As requested, the regexp approach.

    my $string = "ACta"; $string =~ s/^(.*)(.)$/\L$1\U$2/;


    Dave

Re: Implement uclast with a regex
by Not_a_Number (Prior) on Mar 10, 2004 at 19:43 UTC
    What is the best (easiest) way do this?

    If perchance by "best (easiest)" you meant "with the least typing" :-), you could do this:

    $_='ACta'; $_.=uc chop($_=lc); print;

    ...but I imagine I'm far too late...

    dave

Re: Implement uclast with a regex
by bart (Canon) on Mar 11, 2004 at 05:19 UTC
    Yours looks pretty fine to me, almost excellent. I'd make it:
    s/(\w*)(\w)/\L$1\U$2/
Re: Implement uclast with a regex
by BrowserUk (Patriarch) on Mar 11, 2004 at 05:43 UTC
    ($s = lc $s) =~ s[(.)\b][$1&'?']ge

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://335384]
Approved by Corion
Front-paged by dada
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found