[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] string increment



On Sun, 23 Apr 2000, Ronald J Kimball wrote:

> On Mon, Apr 24, 2000 at 05:39:36AM +0200, Arjen Wiersma wrote:
> > 
> > > > perl -e '@a=qw(a a a a); for($i=$#a;-1 < $i; $i--) {
> > > > for($x=0;$x<25;$x++) {$b = ord($a[$i]); $a[$i] = chr(++$b); print
> > > > @a; print "\n"; } }'

This fails to print 'aaaa'.

> How about:
> 
> perl -le '$_ = "aaaa"; for ($i=length($_)-1; $i>=0; --$i) {
>          for $c ("a".."z") { substr($_,$i,1) = $c; print } }'

This will print 'aaaz' twice.  Shortening the range to "b".."z" and adding
a print() to the beginning:

    perl -le 'print $_ = "aaaa"; for ($i=length($_)-1; $i>=0; --$i) {
        for $c ("b".."z") { substr($_,$i,1) = $c; print } }'

Not faster like Arjen wanted, but down to 84 chars:

    perl -le'print$_="aaaa";$i=-1+length;print
        while++substr($_,$i,1)lt"z"or$i--;print'

That's 4 over par.  I bet someone can do better.  Golf anyone? :)

-- 
Neko



==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe