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

Re: [FWP] string increment golf



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


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

perl -le'@A=qw(a a a a);for$i(3,2,1,0){for(b..z){$A[$_]=$c;print@A}}'

perl -le'@A=(a)x4;map{for$c(b..z){$A[$_]=$c;print@A}}3,2,1,0'

"map" is two chars shorter than "for" because you can lose a pair
of parens; You get the length back if you need the iterator variable
to be anything other than $_ though.

map can be used to generate a array of equivalent scalars, sort of
like x on strings -- perldoc perlop -- Hey, x does work on arrays
like I wanted to overload it to....

and substr, although faster than array access, takes far more keys.

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