Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Swap the characters

by sandy1028 (Sexton)
on Apr 14, 2009 at 04:28 UTC ( [id://757327] : perlquestion . print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Swap the characters
by ikegami (Patriarch) on Apr 14, 2009 at 04:34 UTC

    Using regular expression? Presumably you mean using the substitution operator. Given how much this sounds like homework — -- for not disclosing — I'll just give you a tip: Use backreferences.

    Given any number of string only first and last character should be swaped

    The substitution operator can only work on one string at a time. You'll have to use more than the substitution operator if you want to operator on multiple strings.
Re: Swap the characters
by jwkrahn (Abbot) on Apr 14, 2009 at 05:46 UTC
    $ perl -le' my $string = "YANDS"; print reverse $string =~ /(.)(.*)(.)/; ' SANDY
Re: Swap the characters
by BrowserUk (Patriarch) on Apr 14, 2009 at 13:59 UTC
Re: Swap the characters
by citromatik (Curate) on Apr 14, 2009 at 06:43 UTC
    perl -le '$_ = "YANDS";s/(.)(.*)(.)/$3$2$1/ && print'

    citromatik

Re: Swap the characters
by johngg (Canon) on Apr 14, 2009 at 09:57 UTC

    The regex solutions seem to be simplest but here are a couple of alternatives. Using split, join and a slice.

    $ perl -le ' $str = q{yands}; $str = join q{}, ( split m{}, $str )[ -1, 1 .. length( $str ) - 2, 0 ] +; print $str;' sandy $

    Using substr.

    $ perl -le ' $str = q{yands}; substr( $str, 0, 1 ) = substr $str, length( $str ) - 1, 1, substr $str +, 0, 1; print $str;' sandy $

    I hope this is of interest.

    Cheers,

    JohnGG

      $ perl -le ' $str = q{yands}; ( substr( $str, 0, 1 ), substr $str, -1 ) = ( substr( $str, -1 ), subs +tr $str, 0, 1 ); print $str; ' sandy

        I don't know how I came to forget about using a negative offset with substr. Thanks for pointing it out.

        $ perl -le ' $str = q{yands}; substr( $str, 0, 1 ) = substr $str, -1, 1, substr $str, 0, 1; print $str;' sandy $

        Cheers,

        JohnGG

Re: Swap the characters
by johngg (Canon) on Apr 15, 2009 at 15:02 UTC

    Here is a benchmark comparing some of the suggested methods. I run tests before and after the benchmark code to make sure I'm not mangling the source data.

    Here is the output.

    ok 1 - bitStr ok 2 - reRev ok 3 - reSub ok 4 - slice ok 5 - ssBy2s ok 6 - ssNeg Rate slice reSub reRev ssBy2s bitStr ssNeg slice 9667/s -- -21% -39% -56% -66% -78% reSub 12179/s 26% -- -23% -44% -57% -73% reRev 15852/s 64% 30% -- -27% -44% -64% ssBy2s 21779/s 125% 79% 37% -- -23% -51% bitStr 28212/s 192% 132% 78% 30% -- -37% ssNeg 44581/s 361% 266% 181% 105% 58% -- ok 7 - bitStr ok 8 - reRev ok 9 - reSub ok 10 - slice ok 11 - ssBy2s ok 12 - ssNeg 1..12

    spliting, sliceing and joining gets the wooden spoon with the regular expression solutions being a bit faster, reverseing shading substitution. The four-substr ( list ) = ( list ) method is next fastest and the bitwise substr cascade is faster again. Fastest seems to be the assignment to a single lvalue substr of what was replaced in a four-argument substr on the RHS.

    I hope this is of interest.

    Cheers,

    JohnGG

Re: Swap the characters
by talexb (Chancellor) on Apr 15, 2009 at 04:04 UTC
    #!/usr/bin/perl -w # # Split YANDS into SANDY { my $string = 'YANDS'; # First approach .. my @chars = split ( //, $string ); my $result = join ( '', $chars[-1], @chars[1..($#chars-1)], $chars +[0] ); print "1) $string -> $result\n"; # Second approach .. $result = join ( '', reverse ( $string =~ m/(.)(.+)(.)/ ) ); print "2) $string -> $result\n"; }

    Way too much fun. There are so many ways to do it, and I'm sure I could imagine a few more (I wanted to use shift and pop on the array as well ..), but my bed (and some excellent cold medication) is calling.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Swap the characters
by Anonymous Monk on Apr 14, 2009 at 07:38 UTC
    C:\>perl -le"$_=shift; s/\b(\w)(\w*)(\w)\b/$3$2$1/g;print" "12345 Abc +D" 52341 DbcA C:\>perl -le"$_=shift; s/\b(\w)(\w*)(\w)\b/$3$2$1/g;print" "12345 52341 C:\>perl -le"$_=shift; s/\b(\w)(\w*)(\w)\b/$3$2$1/g;print" "AbcD DbcA C:\>perl -le"$_=shift; s/\b(\w)(\w*)(\w)\b/$3$2$1/g;print" "AbcD 1234 +5" DbcA 52341 C:\>
      This will only operate on  \w characters, and I took the requirement of the OPer's homework problem to be to operate on any character. Also, it operates on substrings, and I understood the requirement to be to operate on the entire string.
      >perl -le "$_=shift; s/\b(\w)(\w*)(\w)\b/$3$2$1/g; print" *%$# *%$#