#!/usr/bin/perl -l
# http://perlmonks.org/?node_id=1186402
use strict;
use warnings;
$_ = 12345;
1 while print,
s/.*\K # find the last
(.) # digit such that
(.*) # there is a later (latest)
(.)(??{$1 > $3 and 'x'}) # digit greater than it
(.*) # and get rest
# swap those two digits ( $1 & $3 )
# then reverse everything after the first swapped digit
/ $3 . reverse $2.$1.$4 /xe
Snippets of code should be wrapped in
<code> tags not<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).