Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Sum of N elements in an M element array

by abhay180 (Sexton)
on Feb 02, 2020 at 07:56 UTC ( [id://11112264]=note: print w/replies, xml ) Need Help??


in reply to Re: Sum of N elements in an M element array
in thread Sum of N elements in an M element array

Thanks Corion. The exact problem is about finding all the combinations. I find Math:Combinatorics useful as well. Here is what i did to gather all 2-item combinations from N item array. I wasn't sure how i would do it for any M items.
@a=(1..5); my @comb=""; for(my $i=0;$i<=$#a;$i++) { for(my $j=0;$j<=$#a;$j++) { next if $i==$j; if ($a[$i] < $a[$j]) { push @comb, "$a[$i] , $a[$j]" ; } else { push @comb, "$a[$j] , $a[$i]" ; } } } shift @comb; use List::MoreUtils qw(uniq); @comb = uniq(@comb);

Replies are listed 'Best First'.
Re^3: Sum of N elements in an M element array
by abhay180 (Sexton) on Feb 02, 2020 at 08:04 UTC
    Thanks all for your suggestions. I managed w Math:Combinatorics to get what i want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11112264]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found