GrandFather's solution is the most appropriate one I can think of, but there's something about (.)(.) that bugs me. ...no, not for any good reason. ;)

So here's an inferior solution that doesn't annoy me with (.)(.) (those beady eyes).

my $data = '123456789'; $data =~ s/(.{2})/reverse $1/eg; print $data, "\n";

You didn't define what happens if the string has an uneven number of characters. The most likely alternative is to do nothing with that extra digit at the end. ...but maybe you would prefer doing nothing with the leading digit, in the case of an uneven number of digits, or maybe you'd prefer to fail altogether. ...just some things to consider...

In case you're wondering why this solution is inferior, here's a small list:

Update:
The OP's original solution used substr, and since I was in the mood to tinker, I decided to work up my own substr version:

use strict; use warnings; my $data = '123456789'; my $output = ''; my $position = 0; while ( $position < length $data ) { $output .= reverse substr $data, $position, 2; $position += 2; } print $output, "\n";

Dave


In reply to Re: What is the best solution to swap input data? by davido
in thread What is the best solution to swap input data? by bh_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.