Actually, I tried to patch Data::PowerSet with an option to create adjacent output only:
$ diff -ubB PowerSet.pm.org PowerSet.pm --- PowerSet.pm.org 2008-05-30 11:22:31.000000000 +0200 +++ PowerSet.pm 2008-05-30 12:46:32.000000000 +0200 @@ -9,7 +9,7 @@ use Exporter; use vars qw/$VERSION @ISA @EXPORT_OK/; -$VERSION = '0.05'; +$VERSION = '0.06'; @ISA = ('Exporter'); =head1 NAME @@ -18,8 +18,8 @@ =head1 VERSION -This document describes version 0.05 of Data::PowerSet, released -2008-05-13. +This document describes version 0.06 of Data::PowerSet, released +2008-05-30. =head1 SYNOPSIS @@ -161,6 +161,22 @@ When this attribute is used, the C<next()> method will return a scalar rather than a reference to an array. +=item B<adjacent> + +When this attribute is used, the returned list will contain adjacent +elements only. E.g. + + my $ps = Data::PowerSet->new( {min=>3, adjacent=>1}, 2, 3, 5, 8, 11 + ); + +will produce + + 2, 3, 5, 8, 11 + 2, 3, 5, 8 + 3, 5, 8, 11 + 2, 3, 5 + 3, 5, 8 + 5, 8, 11 + =back =cut @@ -195,6 +211,13 @@ ($args{min}, $args{max}) = ($args{max}, $args{min}) if $args{max} < $args{min}; + + $args{adjacent} = + exists $args{adjacent} + ? $args{adjacent} + : 0 + ; + return bless \%args, $class; } @@ -217,9 +240,18 @@ return undef unless $self->{current} >= 0; my $mask = $self->{current}--; my $offset = 0; + my $last = -1; @set = (); while( $mask ) { + if ($self->{adjacent}) { + if ($mask & 1 && (($offset-1 == $last) || sca +lar(@set) == 0)) { + $last = $offset; + push @set, $self->{data}[$offset]; + } + } else { push @set, $self->{data}[$offset] if $mask & 1; + } + $mask >>= 1; ++$offset; }
However, it produces one element twice, and I can't figure out why.
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

In reply to Re^2: Subsets and adjacent values by andreas1234567
in thread Subsets and adjacent values by andreas1234567

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.