Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

letter mix

by mexnix (Pilgrim)
on Jun 16, 2001 at 00:53 UTC ( #88934=snippet: print w/ replies, xml ) Need Help??

Description: I was looking at premchai21's perlmonk web site, and was decided to go on a quest to sharpen my perl skills. I'm sure this can be done better, and I would love the input. This takes every 2 letters and reverses them.
#!perl -w

use strict;
my $data1 = <DATA>;
my @data2 = split(/(\S.)/, $data1);
foreach my $thn (@data2) {
    $thn =~ s/(\S)(\S)/$2$1/;
}
print @data2;
__END__
rpmehcia12b@gioftoc.mo
Comment on letter mix
Download Code
Re: letter mix
by btrott (Parson) on Jun 16, 2001 at 01:11 UTC
    Couldn't you just do this?
    $data1 =~ s/(\S)(\S)/$2$1/g;
    This takes two non-space characters in a row, and swaps them.

    Perhaps it has slightly different behavior than yours, though. I only tested it on your example.

      I assumed that you had to split every 2 letters, then regex them so that they all swicthed. You know what happens when you assume things...

      __________________________________________________
      %mexnix = (email = > "mexnix@hotmail.com", website => "http://mexnix.perlmonk.org");

Re: letter mix
by John M. Dlugosz (Monsignor) on Jun 16, 2001 at 01:12 UTC
    You said you were interested in other, simpler, solutions, so here is mine: simple use of built-ins, but no real attempt at "golfing" it.

    —John

    while (<DATA>) { chomp; s/(.)(.)/$2$1/g; print "$_\n"; } __DATA__ This is a test. This is the second line. 12ab34cd56ef78gh90ij
Re: letter mix
by mexnix (Pilgrim) on Jun 16, 2001 at 01:57 UTC
      I thought maybe you did the (\S.) on the split and only swapped if both were (\S)(\S) to assure that it would keep word size and still be reversible (that is doing it twice, would get the original input back).

        p

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://88934]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-11-24 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (141 votes), past polls