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

[FWP] all subsets



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