On Tue, Nov 27, 2001 at 02:56:16PM -0800, Jeremy Zawodny wrote:
> Since it's the holiday season, someone recently asked me if I could
> hack out a quick script to setup a "scecret santa" system.  The basic
> idea was this:
> 
>     Feed a script a bunch of e-mail addresses.  The script will assign
>     each person (address) the address of someone they should give a
>     gift to.  Each person only gives one gift and receives one gift.
>     Of course, nobody will give a gift to themselves.  (It should also
>     e-mail everyone automatically so that the person running the
>     script doesn't even know the outcome--but I didn't worry about
>     that part at all.)
> 
> What I managed to hack out was rather ugly.  I'm unhappy with it.
> It just didn't feel like the *right* solution, since it had some odd
> logic in it.  That fact that I was watching TV at the time probably
> didn't help.
> 
> Anyone done this before and come up with something they think is
> particularly elegant or clever?

Using the suggested shuffle and rotating queue...

    shuffle \@emails;
    for (1..@emails) {
        $santa{$emails[0]} = $emails[1];
        push @emails, unshift @emails;
    }
    
    use Mail::Send;
    while( my($santa, $kid) = each %santas ) {
        my $msg = Mail::Send->new( Subject => "Sekret Santa", To => $santa );
        my $fh = $msg->open;
        print $fh "You are the Sekret Santa for $kid\n";
        $fh->close;
    }

-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl Quality Assurance      <[EMAIL PROTECTED]>         Kwalitee Is Job One
Here's hoping you don't become a robot!

Reply via email to