in reply to Subsets and adjacent values

It would be far better to just produce the sets you want than to produce more than you want and then throw some away. The sets you are after are quite easily produced iteratively with a couple of loops:

After the fog cleared:

#! perl -slw use strict; sub adjSets{ my $min = shift; map { my $start = $_; map { [ @_[ $start .. $start+$_ ] ] } $min-1 .. $#_-$start; } 0 .. $#_ - $min; } sub adjSets2{ my $min = shift; my @rv; for my $start ( 0 .. $#_ - $min ) { for my $len ( $min-1 .. $#_-$start ) { push @rv, [ @_[ $start .. $start+$len ] ]; } } return @rv; } print "@$_" for adjSets( 2, 'A'..'E' );; __END__ c:\test>689189 A B A B C A B C D A B C D E B C B C D B C D E C D C D E

Update: Ignore below. T'was too early in my day, so just downvote this. Comments unnecessary :)

Early morning brainfart

#! perl -slw use strict; sub adjSets{ my $min = shift; map { my $start = $_; map { [ @_[ $start .. $_ ] ] } $min .. $#_; } 0 .. $#_ - $min; } print "@$_" for adjSets( 2, 'A'..'E' );; __END__ A B C A B C D A B C D E B C B C D B C D E C C D C D E

A less idiomatic version:

sub adjSets2{ my $min = shift; my @rv; for my $start ( 0 .. $#_ - $min ) { for my $len ( $min .. $#_ ) { push @rv, [ @_[ $start .. $len ] ]; } } return @rv; }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Subsets and adjacent values
by andreas1234567 (Vicar) on Jun 02, 2008 at 09:14 UTC
    Thanks a lot. However, it seems like it misses the last iteration (but that is easily fixed):
    < } 0 .. $#_ - $min; --- > } 0 .. $#_ - $min + 1;
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]