Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

letter mix

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

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

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 = > "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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found