use warnings; use strict; my %combinations; #hash which holds combination pairs print "Preprocessing data...\n"; while (<>) { chomp; my @line=split; for my $number (@line) { for (@line) { if ($number != $_) { #Disallow combos like (1,1) $combinations{$number}{$_}++; #I saw combo ($number, $_) } } } } for my $number (keys %combinations) { $combinations{$number}=[sort {$combinations{$number}{$b} <=> $combinations{$number}{$a}} (keys %{$combinations{$number}})]; #replace our anonymous hash with an anonymous array containing the combo pairs in a greatest-to-least order. } print "Hash Initialized. Enter: (q to quit)\n"; while () { chomp; last if /q/; my ($number, $quantity) = split; print "\n"; print "($number, $_)\n" for (@{$combinations{$number}}[0..$quantity-1]); }