Skip to content

Instantly share code, notes, and snippets.

<cffunction name="calculateUpcCheckDigit" output="no" returnType="string">
<cfargument name="upc" required="true" type="string">
<cfset oddnums = 0>
<cfset evennums = 0>
<cfloop from="1" to="#len(upc)#" index="x">
<cfif x mod 2 neq 0>
<cfset oddnums = oddnums + mid(upc, x, 1)>
</cfif>
</cfloop>
<cfloop from="1" to="#len(upc)#" index="x">
@pud
pud / gist:5235862
Last active December 15, 2015 09:09
Convert a query result to JSON in ColdFusion
<cffunction name="QueryToStructLowerCase" access="public" returntype="any" output="false"
hint="Converts an entire query or the given record to a struct. This might return a structure (single record) or an array of structures.">
<cfargument name="Query" type="query" required="true" />
<cfargument name="Row" type="numeric" required="false" default="0" />
<!--- Thanks to: http://www.bennadel.com/index.cfm?event=blog.view&id=149 --->
<cfscript>
var local.local = StructNew();
if (ARGUMENTS.Row){
local.local.FromIndex = ARGUMENTS.Row;
local.local.ToIndex = ARGUMENTS.Row;
@pud
pud / gist:7075368
Last active December 26, 2015 01:59
Get user's real IP address behind an Amazon AWS load balancer.
<cfif structkeyexists(GetHttpRequestData().headers,"X-Forwarded-For")>
<!--- ipaddress if user is behind load balancer --->
<cfset my_ip = #GetHttpRequestData().headers['X-Forwarded-For']#>
<cfset my_ip = trim(listgetat(my_ip,1))>
<cfelse>
<!--- ipaddress if user is not behind load balancer --->
<cfset my_ip = cgi.remote_addr>
</cfif>
<cfoutput>
<!---
convert comma separated values (CSV) to tab-separated values (TSV)
while preserving commas in qualified strings
--->
<cffunction name="CSVtoTSV" output="no">
<cfargument name="csv" required="yes" type="string">
<cfset csv = replace(csv, ',', ', ', 'all')> <!--- make sure to trim() values when you use them later --->