#!/usr/bin/perl use strict; use warnings; my @typeset = qw(P K L G); my $length = 10; strangemultiset(\@typeset,$length); sub strangemultiset { my ($typeset,$length)=@_; my @lst=map{1}@$typeset; while(@lst <= @$typeset) { my $len=0; $len+=$_ for(@lst); if($len >= $length) { my @out; push(@out, ($typeset[$_]) x $lst[$_]) for(0..$#$typeset); print join('.',@out)."\n"; } $lst[0]++; for my $p (0..$#lst) { my $left = 0; $left += $_ for(@lst); if($left > $length) { $lst[$p] = 1; $lst[$p+1] ++ ; } } } }