in reply to YET another regexp puzzle

This is an obvious candidate for substr(). The following code just takes the substring of every pair of sequential characters and replaces each set with the reverse of that substring, effectively swapping the 2 characters.

#!c:/perl/bin/perl -w $|++; use strict; my $word = 'jigglewort'; for my $i (0 .. length($word)-1) { my $tmp = $word; substr($tmp, $i, 2, reverse substr($tmp, $i, 2)); # _or_ # substr($tmp, $i, 2) = reverse substr($tmp, $i, 2); print $tmp, "\n"; } =for output ijgglewort jgiglewort jigglewort jiglgewort jiggelwort jigglweort jiggleowrt jigglewrot jigglewotr jigglewort =cut

Update: Added lvalue substr as an option :)

Replies are listed 'Best First'.
Re: Re: YET another regexp puzzle
by carric (Beadle) on Nov 20, 2003 at 03:08 UTC
    SWEET!

    That was the ticket! I really appreciate that.. now my brain can quit hurting, and I'll add that to my tool kit.