I was intrigued and found Math::Fleximal and leveraged it into the following code based on MidLifeXis proposed solution.

#! C:/Perl/bin/perl ### Test for x objects in y containers where all objects are used ### Treats the containers y as n in a base n system and the objects x +as the digits use strict; use warnings; use Math::Fleximal; #~ use Smart::Comments '###';#, '####' use YAML; $| = 1; my ( $Containers, $ObjectCount, $ContArrayRef, $AnswerHashRef ) = (); #### Get the container count while ( !$Containers ) { print "Please enter a positive number of Containers "; $Containers = <>; chomp $Containers; ### $Containers if ( $Containers ) { push @$ContArrayRef, "WBox$_" for (1..$Containers); ### $ContArrayRef $AnswerHashRef->{'boxes'} = $Containers; } } #### Get the object count my $CorrectObjectCount = 0; while ( $CorrectObjectCount == 0 or !$ObjectCount ) { print "Please enter the number of Objects "; $ObjectCount = <>; chomp $ObjectCount; if ($ObjectCount >= $Containers) { $CorrectObjectCount = 1; ### $ObjectCount $AnswerHashRef->{'objects'} = $ObjectCount; } else { print "You have not chosen enough objects to fill all containe +rs\n"; } } #### Iterate through all possible combinations of all digits #### Start at 0 my $CountFleximal = Math::Fleximal->new( $ContArrayRef->[0], $ContA +rrayRef ); #### Fleck value one my $OneFleximal = $CountFleximal->one(); #### $OneFleximal #### build the Max Fleck for iteration my $MaxFlex; $MaxFlex .= $ContArrayRef->[$Containers - 1] for (1..$ObjectCount); ### $MaxFlex my $MaxFleximal = Math::Fleximal->new( $MaxFlex, $ContArrayRef +); #### Iterate through all possiblities while ( $CountFleximal->cmp($MaxFleximal) < 1) { #### sprintif for Fleximal to fill all digits my $SprintfFleximal; my @FleximalDigits = split "W", $CountFleximal->to_str; #### @FleximalDigits for (1..$ObjectCount) { my $NextFleck = pop @FleximalDigits; $NextFleck ||= $ContArrayRef->[0] ; $NextFleck =~ s/W(.+)/$1/; $SprintfFleximal = ( $SprintfFleximal ) ? $NextFleck . $SprintfFleximal : $NextFleck ; } ### $SprintfFleximal ### Check that all containers have objects my $test = 1; for my $i (1..$Containers) { if ( $SprintfFleximal !~ /Box$i/ ) { $test = 0 ; ### Box not represented: $i } } ### Load the answer to the output if valid if ( $test == 1 ) { my @ContainerOut = split "Box", $SprintfFleximal; shift @ContainerOut; ### @ContainerOut for my $object (0..$ObjectCount-1) { push @{$AnswerHashRef->{$SprintfFleximal}->{"Box". $Contai +nerOut[$object]}}, $object +1; } } $CountFleximal = $CountFleximal->add( $OneFleximal );#++ routine } ### $AnswerHashRef YAML::DumpFile ( 'Answer.txt', $AnswerHashRef ); 1;

In reply to Re^2: x objects in y containers where all objects are used by jandrew
in thread x objects in y containers where all objects are used by Ectaris

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.