in reply to What is the best solution to swap input data?
use strict; use warnings; my $str = q{12345678}; print qq{$str\n}; $str = revByGroup($str); print qq{$str\n}; sub revByGroup { my $str = shift; my @chars = split m{}, $str; for (my $idx = 0; $idx < $#chars; $idx += 2) { splice @chars, $idx, 0, splice @chars, $idx + 1, 1; } return join q{}, @chars; }
and the output is
12345678 21436587
It copes with an odd numer of characters, leaving the last odd character at the end of the string. I hope this is of interest.
Cheers,
JohnGG
|
|---|