#! /usr/bin/perl -w use strict; use warnings; my %people = ( jim => ['betty', 'claire'], betty => ['frank', 'jim' ], frank => ['claire', 'john' ], claire => ['john', 'betty' ], john => ['jim', 'frank' ], nancy => ['', '' ], # new girl ); my @allPeople = sort keys %people; my %recipients; @recipients{@allPeople}=undef; srand; # randomize! foreach (@allPeople) { my @remainingPeople = keys %recipients; my $randPerson; my $iterations; do { $randPerson = $remainingPeople[int (rand ($#remainingPeople))]; $iterations++; die ("ERROR: Failed to generate list\n") unless ($iterations <$#allPeople); } until (testRecipient($_, $randPerson)); print "$_ can give to $randPerson\n"; delete $recipients {$randPerson}; } sub testRecipient { my ($santa, $recipient) = @_; my $arrayref = $people{$santa}; # test for self giving return 0 if ($santa eq $recipient); #test for giving to the given foreach (@$arrayref){ return 0 if ($_ eq $recipient); } return 1; }