Saturday, November 26, 2005

 A touch of overkill 

Our family does a random drawing every year, around Thanksgiving time, to determine who gives a present to whom for Christmas. (In the interest of not engaging in total consumerist gluttony, we try to reduce the total amount and cost of gifts exchanged in the holiday season.) We've always cut up little pieces of paper, and drawn them from a basket to assign the names. Pretty 19th-century, don't you think?

So, with plenty of free time at hand, I whipped up a couple Perl scripts to make these random assignments for us. This one takes a list of names of assigns gift recipients to each one randomly:

#!/usr/bin/perl -w

@givers = ('Dan','Lori','Connie','Dave','Chet','Cindy','Eric','Mom','Kent');
@receivers = @givers ;

foreach $giver (@givers) {
$num_receiver = int(rand(@receivers)) ;
$receiver = $receivers[ $num_receiver ];
redo if $giver eq $receiver;
print "======++++++ $giver will give a present to $receiver\n" ;
splice (@receivers,$num_receiver,1);

My mother also suggested today that we come up with a similar scheme for having the adults give a gift to one child. The constraint was that a parent could not give a gift to their own child, but other than that, every assignment was random. I also cranked out a script for that:


my $adult;
my $kid;
my $num;

my %parents = (
'Gabby' => ['Dan','Lori'],
'Grant' => ['Dan','Lori'],
'Alissa' => ['Dan','Lori'],
'Bianca' => ['Dan','Lori'],
'Caitlin' => ['Dan','Lori'],
'Zack' => ['Connie','Dave'],
'Nick' => ['Connie','Dave'],

my @adults = ('Dan','Lori','Connie','Dave','Chet','Cindy','Eric');
my @kids = ("Gabby","Grant","Alissa","Bianca","Caitlin","Zack","Nick");

BLOCK: foreach $child (@kids) {
$num = int(rand(@adults));
$adult = $adults[ $num ];
foreach $parent ( @{$parents{ $child }} ) {
redo BLOCK if $parent eq $adult ;
print "======++++++ $child will get a present from $adult\n" ;
splice (@adults,$num,1);

As it turns out, we probably won't even use this second script, since there are some issues about the purpose of even doing this random assignment thingy in the first place. But I felt compelled to prove to myself that I can still create something from scratch on a moments notice, even after a full year away from programming at Yahoo. Thanks to Apple and Mac OS X, where I can easily graft together Perl in a UNIX environment.

Comments: Post a Comment

This page is powered by Blogger. Isn't yours?