selecting for of them ... no numbers left in the array

Assuming that "for of them" means "four of them", then you have an immediate problem in that if your array contains 50 (or 51, or 53, or 54 or 55 etc. ) values, then you will eventually reach a point where there are less than four left to pick. What then?

The ultimate purpose is to find all sort of combinations,

Do you mean "all possible combinations"? If so, Do you mean "all possible sets of 4 that sum to 15" or "all possible sets of sets of 4 that sum to 15"?

What if the array of numbers doesn't contain any set of four that sums to 15?

Some code for you to play with. It generates an array of 50 small numbers (0..9) and then attempts to empty the array by picking and removing sets of 4. It will never terminate (use ^C), but might illustrate some ways of approaching whichever clarification of your problem is the correct one:)

#! perl -slw use strict; use List::Util qw[ shuffle sum ]; my @nums = map int rand 10, 1 .. 50; my $n = 0; { my @pick = (shuffle 0 .. $#nums )[ 0 .. 3 ]; if( 15 == sum @nums[ @pick ] ) { print "\n@nums[ @pick ]"; splice @nums, $_, 1 for sort{ $b <=> $a } @pick; $n = 0; } printf "\rtrying to find another from [@nums]: %d", ++$n; redo if @nums > 4; } __END__ c:\test>840204.pl trying to find another from [5 3 5 0 1 8 3 6 1 5 7 5 3 1 6 4 5 9 5 0 1 + 6 1 3 5 0 9 1 7 8 0 7 4 9 7 5 6 0 2 3 7 9 6 1 7 7 2 0 4 9]: 3 3 8 4 0 trying to find another from [5 3 5 0 1 8 3 6 1 5 7 5 1 6 5 9 5 0 1 6 1 + 3 5 0 9 1 7 0 7 4 9 7 5 6 0 2 3 7 9 6 1 7 7 2 4 9]: 16 5 6 3 1 trying to find another from [5 3 0 1 8 3 6 1 5 7 5 1 5 9 5 0 1 6 1 3 5 + 0 9 7 0 7 4 9 7 5 6 0 2 7 9 6 1 7 7 2 4 9]: 24 7 0 7 1 trying to find another from [5 3 0 8 3 6 1 5 5 1 5 9 5 0 1 6 1 3 5 0 9 + 7 7 4 9 7 5 6 0 2 7 9 6 1 7 2 4 9]: 45 0 5 4 6 trying to find another from [3 0 8 3 6 1 5 5 1 5 9 5 0 1 6 1 3 5 0 9 7 + 7 4 9 7 5 6 2 7 9 1 7 2 9]: 9 0 3 7 5 trying to find another from [3 0 8 3 6 1 5 1 5 9 5 1 6 1 5 0 9 7 7 4 9 + 7 5 6 2 7 9 1 2 9]: 1 7 2 1 5 trying to find another from [3 0 8 3 6 1 5 1 9 5 1 6 5 0 9 7 7 4 9 7 5 + 6 2 9 1 9]: 41 1 3 6 5 trying to find another from [3 0 8 6 1 9 5 1 5 0 9 7 7 4 9 7 5 6 2 9 1 + 9]: 5 7 0 7 1 trying to find another from [3 8 6 1 9 5 5 0 9 4 9 7 5 6 2 9 1 9]: 3 8 0 6 1 trying to find another from [3 6 9 5 5 9 4 9 7 5 2 9 1 9]: 86 5 2 1 7 trying to find another from [3 6 9 5 9 4 9 5 9 9]: 85635 ^C c:\test>840204.pl trying to find another from [3 9 9 7 4 8 5 7 0 1 4 7 8 0 8 7 5 2 3 4 1 + 6 6 3 2 5 7 5 8 5 4 4 3 5 4 6 6 6 5 3 5 3 4 0 3 4 5 1 7 4]: 10 4 0 3 8 trying to find another from [3 9 9 7 4 8 5 7 0 1 4 7 8 0 8 7 5 2 3 4 1 + 6 6 3 2 5 7 5 5 4 3 5 4 6 6 6 5 5 3 4 3 4 5 1 7 4]: 1 4 4 1 6 trying to find another from [3 9 9 7 4 8 5 7 0 1 4 7 8 0 8 7 5 2 3 1 6 + 6 3 2 5 7 5 5 4 3 5 4 6 6 5 5 3 4 3 5 7 4]: 7 5 5 5 0 trying to find another from [3 9 9 7 4 8 7 1 4 7 8 0 8 7 2 3 1 6 6 3 2 + 5 7 5 5 4 3 5 4 6 6 5 3 4 3 5 7 4]: 2 3 5 3 4 trying to find another from [9 9 7 4 8 7 1 7 8 0 8 7 2 1 6 6 3 2 5 7 5 + 5 4 3 4 6 6 5 3 4 3 5 7 4]: 10 1 2 4 8 trying to find another from [9 9 7 4 8 7 1 7 8 0 7 6 6 3 2 5 7 5 5 4 3 + 6 6 5 3 4 3 5 7 4]: 6 3 5 4 3 trying to find another from [9 9 7 4 8 7 1 7 8 0 7 6 6 2 7 5 5 4 3 6 6 + 5 3 5 7 4]: 22 5 0 6 4 trying to find another from [9 9 7 4 8 7 1 7 8 7 6 2 7 5 5 4 3 6 6 5 3 + 7]: 31 7 4 3 1 trying to find another from [9 9 7 4 8 7 8 7 6 2 7 5 5 6 6 5 3 7]: 138 5 2 3 5 trying to find another from [9 9 7 4 8 7 8 7 6 7 6 6 5 7]: 74781 ^C

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.
RIP an inspiration; A true Folk's Guy

In reply to Re: Randomly select values from array by BrowserUk
in thread Randomly select values from array by Anonymous Monk

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.