CFLib.org – Common Function Library Project

StructInvert(st)

Last updated November 13, 2001

author

Craig Fisher

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Takes a struct of simple values and returns the structure with the values and keys inverted. The Values of the structure passed in will be the keys of the reutrned structure. The Values of the Keys of the returned structure will be the corresponding keys of the passed in structure.

Return Values:
Returns a structure.

Example:

<cfscript>
	stOne=StructNew();
	stOne.firstvalue="cheese";
	stOne.secondvalue="soup";
	stOne.thirdvalue="donuts";
	stOneInverted=StructInvert(stOne);
</cfscript>
stOne:
<cfdump var="#stOne#">
stOneInverted:
<cfdump var="#stOneInverted#">

Parameters:

Name Description Required
st Structure of simple name/value pairs you want inverted. Yes

Full UDF Source:

/**
 * Takes a struct of simple values and returns the structure with the values and keys inverted.
 * 
 * @param st 	 Structure of simple name/value pairs you want inverted. 
 * @return Returns a structure. 
 * @author Craig Fisher (craig@altainteractive.com) 
 * @version 1, November 13, 2001 
 */
function StructInvert(st) {
		var stn=StructNew();
		var lKeys="";
		var nkey="";
		var i=1;
		var eflg=0;
		if (NOT IsStruct(st)) {
			eflg=1;
		}
		else {
			lKeys=StructKeyList(st);
			for (i=1; i LTE ListLen(lKeys); i=i+1) {
				nKey=listgetat(lkeys, i);
				if (IsSimpleValue(st[nKey]))
					stn[st[nKey]]=nKey;
				else {
					eflg=1;
					break;
				}
			}
		}
		if (eflg is 1) {
			writeoutput("Error in <Code>InvertStruct()</code>! Correct usage: InvertStruct(<I>Structure</I>) -- Returns a structure with the values and keys of <I>Structure</I> inverted when <i>Structure</i> is a structure of simple values.");
			return 0;
		}
		else {
			return stn;
		}
	}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson