in reply to What is the best solution to swap input data?

You can also do this with split, although my implementation is not quite optimal.

#!/usr/bin/perl use strict; use warnings; while(<DATA>){ chomp; my $in = $_; my @pairs = split(/(.{2})/); $_ = reverse foreach @pairs; my $out = join('', @pairs); print "in: $in => out: $out\n"; } __DATA__ 12345678 1234 12 1234 123456 87654321

Outputs:

in: 12345678 => out: 21436587 in: 1234 => out: 2143 in: 12 => out: 21 in: 1234 => out: 2143 in: 123456 => out: 214365 in: 87654321 => out: 78563412

As I said, I'm quite sure that this is far from optimal, as the regex is probably quicker, shorter, and at least as clear.

emc

At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

—Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

Replies are listed 'Best First'.
Re^2: What is the best solution to swap input data?
by johngg (Canon) on Nov 09, 2006 at 23:22 UTC
    You are right, the regex is quicker but not the quickest. That honour goes davido's and fenLisesi's substr methods which are neck and neck. My split, splice and join gets the wooden spoon, being desperately slower than any other method.

    I had also come up with almost the same method as your's but using a map rather than storing in an array. Your version is slightly the faster. One way of speeding both up is to grep out the empty elements that result from the split. The benchmark code first works on a short odd-length string to check that all methods produce the same result. Then methods are compared with a longer string. Here's the results

    reRev - 214365879 reSwap - 214365879 spliceSwap - 214365879 splitMap - 214365879 splitMapGrep - 214365879 splitRev - 214365879 splitRevGrep - 214365879 substrRev - 214365879 substrSubstr - 214365879 twoSubstrs - 214365879 Rate SpliceSwap SplitMap SplitRev TwoSubstrs SplitMapGr +ep SplitRevGrep ReRev ReSwap SubstrRev SubstrSubstr SpliceSwap 4.15/s -- -64% -68% -70% -7 +1% -72% -84% -85% -89% -89% SplitMap 11.7/s 181% -- -9% -17% -1 +9% -22% -54% -59% -68% -68% SplitRev 12.8/s 209% 10% -- -8% -1 +1% -14% -50% -55% -65% -65% TwoSubstrs 14.0/s 237% 20% 9% -- - +3% -7% -45% -50% -62% -62% SplitMapGrep 14.4/s 247% 23% 13% 3% +-- -4% -44% -49% -61% -61% SplitRevGrep 15.0/s 261% 28% 17% 7% +4% -- -42% -47% -59% -59% ReRev 25.6/s 517% 119% 100% 83% 7 +8% 71% -- -9% -30% -30% ReSwap 28.3/s 580% 142% 120% 102% 9 +6% 89% 10% -- -23% -23% SubstrRev 36.7/s 785% 214% 187% 162% 15 +5% 145% 43% 30% -- -0% SubstrSubstr 36.9/s 788% 215% 188% 163% 15 +6% 146% 44% 30% 0% --

    and here's the code

    I hope this is of interest.

    Cheers,

    JohnGG

      For thorough evaluation of alternatives, johngg++

      emc

      At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

      —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.