Hello fellow monks!
After a long ( and now semi permanent ) exile from computing I present a subroutine which will populate an array with all possible case permutations of a word which is supplied to it. I consider it an elegant solution to something which originally was a recursive broken monstrosity.
After a long ( and now semi permanent ) exile from computing I present a subroutine which will populate an array with all possible case permutations of a word which is supplied to it. I consider it an elegant solution to something which originally was a recursive broken monstrosity.
Recursion is evil.
sub casify { my @arr = (); my $word =lc $_[0]; my $maxcombos = 2**(length $word) - 1; foreach $n ( 0..$maxcombos ) { # convert number from being stringified, to being a u_short, and then +into binary $_ = unpack( 'b*', pack('s',$n) ); tr/1/_/; # uppercase ones tr/0/\377/; # leave zeros alone $result = "$word" & $_; # string AND push( @arr, $result ) ; } return @arr; } #-------------------------------------------------- # fast casify(), first arg word ref, second arg arr ref sub casify_fast { foreach $n ( 0..(2**(length ${$_[0]}) - 1) ) { # convert number from being stringified, to being a u_short, and then +into binary $_ = unpack( 'b*', pack('s',$n) ); tr/10/_\377/; push( @{$_[1]}, ("${$_[0]}" & $_) ) ; # string AND them, ones up +percase the letters, zeros don't } return; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: upper-lower case combinatorics
by Aristotle (Chancellor) on Dec 24, 2004 at 02:56 UTC | |
Re: upper-lower case combinatorics
by bl0rf (Pilgrim) on Dec 24, 2004 at 21:33 UTC |
Back to
Cool Uses for Perl