Skip to content

Instantly share code, notes, and snippets.

@paulklinkenberg
Created February 22, 2011 21:26
Show Gist options
  • Save paulklinkenberg/839431 to your computer and use it in GitHub Desktop.
Save paulklinkenberg/839431 to your computer and use it in GitHub Desktop.
Get random winner
<cfhttp url="http://blog.bittersweetryan.com/2011/02/post-some-cf-code-on-github-and-win.html" />
<cfset possibleWinners = {} />
<cfset commentsHtmlArr = rematch("<dt class='comment-author.*?</dd>", rereplace(cfhttp.filecontent, '[\r\n]+', '', 'all')) />
<cfset gistURLRegex = "https://gist\.github\.com/[0-9]+" />
<cfloop from="1" to="#arrayLen(commentsHtmlArr)#" index="arrIndex">
<cfset currHtml = commentsHtmlArr[arrIndex] />
<cfif refindNoCase(gistURLRegex, currHtml)>
<cfset gistURL = reMatchNoCase(gistURLRegex, currHtml) />
<cfset gistURL = gistURL[1] />
<cfif not structKeyExists(possibleWinners, gistURL)>
<cfset userName = rereplace(currHtml, '^.*?<img.*?title="(.*?)".+', '\1') />
<cfset possibleWinners[gistURL] = userName />
</cfif>
</cfif>
</cfloop>
<cfif structCount(possibleWinners) gt 1>
<!--- get a random number from random.org --->
<cfhttp url="http://www.random.org/integers/?num=1&min=1&max=#structCount(possibleWinners)#&col=1&base=10&format=plain&rnd=new" />
<cfset theWinningURL = listGetAt(structKeyList(possibleWinners), val(trim(cfhttp.filecontent))) />
<cfelse>
<!--- I was the only one participating :/ --->
<cfset theWinningURL = structKeyList(possibleWinners) />
</cfif>
<cfoutput><p>The winner is <strong>#possibleWinners[theWinningURL]#</strong>, by submitting <a href="#theWinningURL#" target="_blank">his incredible code</a>!
</p></cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment