Just for fun/whatever insight it might bring, here's a recursive, no-modules solution that generates only the matching combinations.
use warnings; use strict; # A mech is an arrayref of three attributes: name, weight, 'clan' my @mechs = map [split], <DATA>; # find all lances with M members and <=W weight my @possible_lances = find_lances (3, 150, @mechs); print 'Found '.@possible_lances, " sets\n"; print join(',', map($_->[0], @$_)), "\n" for @possible_lances; # Return an array of arrayrefs. Each arrayref contains a set (lance) o +f mechs. sub find_lances { my ($M, $W, $first, @rest) = @_; # If there aren't enough mechs left, or you're overweight, no matc +h if ($M < 1 or $M-1 > @rest or $W < 0) { return () } # Base case elsif ($M == 1) { return(map [$_], grep { $_->[1] <= $W } $first, @rest); } else { return ( # All qualifying sets that include the 1st element do { if ($first->[1] <= $W) { map [$first, @$_], find_lances($M-1, $W-$first->[1], @rest); } else { () } }, # and all qualifying sets that do not include the 1st +element find_lances($M, $W, @rest) ); } } __DATA__ Flea 20 clan Commando 25 clan UrbanMech 30 clan Hollander 35 clan Jenner 35 clan Raven 35 clan Wolfhound 35 clan BlackHawk 50 clan Hunchback 50 clan Rifleman 60 clan Catapult 65 clan Loki 65 clan Thor 70 clan MadCat 75 clan Gargoyle 80 clan Victor 80 clan Zeus 80 clan Longbow 85 clan Warhawk 85 clan Mauler 90 clan Atlas 100 clan

Caution: Contents may have been coded under pressure.

In reply to Re: find combinations by Roy Johnson
in thread find combinations by holli

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.