I think this does the same thing as your code, but is a bit simpler.

#! perl -slw use strict; use warnings; my @array = sort map{ 1+ int rand(8) } 1..36; print "@array"; print '=' x 30; my %X; $X{$_}++ for @array; my @keys = sort { $a <=> $b } keys %X; my %hash; my @pairs; foreach my $x ( @keys ){ foreach my $y ( @keys ){ next if not ( $X{$x} and $X{$y} ) or $x eq $y or defined $hash{$x}{$y}; push @pairs, [$x,$y]; print scalar @pairs, " = $x $y"; $hash{$x}{$y} = $hash{$y}{$x} = 1; $X{$x} -= 1; $X{$y} -= 1; } } print 'Count: ', scalar @pairs;

Why do you use references to anonymous hashes instead of a straight hash? All it does it complicate the syntax for no benefit.

To extend your solution to more than two elements per row, you might be able to use tye's Algorithm::Loops.

I say "might" because your description of the problem is somewhat confusing. You say "A Pair is a set of 2 numbers", "A Row is a set of 'P' such pairs", & "P is a fixed number : Let's take it as 2", which by my reading should mean that each Row (line) in your sample output would consist of P pairs, ie. 2 sets of 2 numbers, but your output only shows one pair?

Overall, I found your description of the problem completely confusing.

What I think you are trying to do is:

Generate the maximum number of unique sets of N unique numbers from a given array of numbers, using each of the (possibly duplicated) numbers in the input array exactly as many times as it appears in the input array.

Does that sum up your intent?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: Pairing the pairs by BrowserUk
in thread Pairing the pairs by artist

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.