#! 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;