[Date Prev][Date Next][Thread Prev][Thread Next]
[Search]
[Date Index]
[Thread Index]
[FWP] all subsets
- To: fwp@technofile.org
- Subject: [FWP] all subsets
- From: Jeff Pinyan <jeffp@crusoe.net>
- Date: Mon, 14 May 2001 19:32:35 -0400 (EDT)
- Delivered-To: irons-bumppo:net-mpfwp@bumppo.net
Wow. I saw some awesome code today from someone on DALnet #perl:
perl -wnle 'split//;for$x(1..1<<@_){print@_[grep $x&1<<$_,0..$#_]}'
That prints all subsets of a string (except for "", I believe). You can
follow it to see how.
So I decided I wanted to do that with a regex.
And I *did*.
use re 'eval';
$_ = "abcdef";
my $pat = join '', map "(?:(.)|.)", 1 .. length;
my $str = join '', map "\$$_", 1 .. length;
my $print = qq{(?{ print "$str\n" })};
/$pat$print(?!)/;
That was so much fun. :)
--
Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
==== Want to unsubscribe from Fun With Perl? Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
==== unsubscribe