Benji Flaming

25 CD giveaway

In keeping with the proverbial “Christmas spirit”, this December 25th I will be mailing out 25 copies of my solo banjo CD to randomly-selected subscribers to my e-mail list. I’ll also be sending everyone on my e-mail list a link to download an MP3 of my solo banjo Carol of the Bells arrangement.

If you are already subscribed, but you aren’t sure whether you provided your zip/postal code when you signed up (the random-selection program will ignore entries with no zip/postal code), you can check or update your subscription info at:

http://BenjiFlaming.FanBridge.com/

If you are interested in the precise technical details of how the selection process will be handled, below is the complete code listing. It is written in Ruby, although it is not written in particularly good Ruby. I’m a little out-of-practice at the moment, but this is what I cooked up in an hour:


#!/usr/bin/env ruby
# choosewinner.rb

require 'csv'

# These could be parsed from the command-line, if I wrote Ruby more often.
filename = 'entries.csv'
winner_list_filename = 'winners.csv'
winner_count = 25

# empty arrays
entries = []
eligible = []
winners = []


puts "Opening and processing #{filename}"
CSV.open(filename, 'r') do |row|
 entries << row
end
puts "Read #{entries.length} lines"


# The first line of the CSV file contains the column names.  We remove it from
# the list of entries, and extract the column indices from it.
column_names = entries.shift
zipcode = column_names.index("zip")
email = column_names.index("email")
firstname = column_names.index("firstname")
lastname = column_names.index("lastname")


# discard entries which are missing a zipcode or e-mail address
entries.each do |entry|
 eligible << entry if entry[zipcode] and entry[email]
end
puts "Found #{eligible.length} eligible entries out of #{entries.length} total"


# randomly select each winning entry, and remove it from the pool
winner_count.times do
 winner_index = rand(eligible.length)
 winners << eligible[winner_index]
 eligible.delete_at(winner_index)
end
puts "Got #{winners.length} winners:"
puts "----------------------------------------"


# display a summary onscreen, and write the details to a file
File.open(winner_list_filename, 'w') do |winner_list|

 # preserve column names from original CSV file
 winner_list.puts column_names.join(',')

 winners.each do |winner|
  puts "#{winner[firstname]} #{winner[lastname]} #{winner[email]}"
  winner_list.puts winner.join(',')
 end
end
puts "----------------------------------------"
puts "Detailed results written to #{winner_list_filename}"

(Archived blog post from 2010-12-15)

Where to next?

View the next blog post
View the previous blog post
Browse all blog posts
View a random blog post
View the next post by date
Follow and support my work
Visit the homepage

Copyright © 1998-2024 Benjamin Flaming
All rights reserved.
This website does not use cookies.
Some 3rd-party music or video players
used here may set their own cookies.