Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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;
Comment on letter mix
Download Code
Replies are listed 'Best First'.
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 = > "", website => "");

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.


    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).


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2016-04-30 05:10 GMT
Find Nodes?
    Voting Booth?
    :nehw tseb si esrever ni gnitirW

    Results (441 votes). Check out past polls.