CFLib.org – Common Function Library Project

Dump(var [, expand] [, label] [, top])

Last updated October 16, 2002
Download UDF

author

Raymond Camden                                    Raymond Camden

Version: 2 | Requires: ColdFusion MX | Library: CFMLLib

Description:
Mimics the cfdump tag, but adds new "top" attribute.

Return Values:
Returns a string.

Example:

<cfset x = arraynew(1)>
<cfloop index="y" from=1 to=200>
    <cfset x[y] = randRange(1,1000)>
</cfloop>

<cfscript>
dump(var=x,label="The Array X",top=10);
</cfscript>

Parameters:

Name Description Required
var The variable to dump. Yes
expand Expand output. Defaults to true. No
label Label for dump. No
top Restricts output based on type. If array or query, top will represent the number of rows to show. If structure, will show this many keys. No

Full UDF Source:

<!---
Mimics the cfdump tag.
Updated for final cfmx var scope - also, we only redo var if size bigger than top.

@param var      The variable to dump. (Required)
@param expand      Expand output. Defaults to true. (Optional)
@param label      Label for dump. (Optional)
@param top      Restricts output based on type. If array or query, top will represent the number of rows to show. If structure, will show this many keys. (Optional)
@return Returns a string.
@author Raymond Camden (ray@camdenfamily.com)
@version 2, October 16, 2002
--->

<cffunction name="dump" returnType="string">
    <cfargument name="var" type="any" required="true">
    <cfargument name="expand" type="boolean" required="false" default="true">
    <cfargument name="label" type="string" required="false" default="">
    <cfargument name="top" type="numeric" required="false">
    
    <!--- var --->
<cfset var type = "">
<cfset var tempArray = arrayNew(1)>
<cfset var temp_x = 1>
<cfset var tempStruct = structNew()>
    <cfset var orderedKeys = "">
    <cfset var tempQuery = queryNew("")>
    <cfset var col = "">
    
    <!--- do filtering if top ---->
    <cfif isDefined("top")>
    
        <cfif isArray(var)>
            <cfset type = "array">
        </cfif>
        <cfif isStruct(var)>
            <cfset type="struct">
        </cfif>
        <cfif isQuery(var)>
            <cfset type="query">
        </cfif>
        
        <cfswitch expression="#type#">
        
            <cfcase value="array">
                <cfif arrayLen(var) gt top>
                    <cfloop index="temp_x" from=1 to="#Min(arrayLen(var),top)#">
                        <cfset tempArray[temp_x] = var[temp_x]>
                    </cfloop>
                    <cfset var = tempArray>
                </cfif>
            </cfcase>
            
            <cfcase value="struct">
                <cfif listLen(structKeyList(var)) gt top>
                    <cfset orderedKeys = listSort(structKeyList(var),"text")>
                    <cfloop index="temp_x" from=1 to="#Min(listLen(orderedKeys),top)#">
                        <cfset tempStruct[listGetAt(orderedKeys,temp_x)] = var[listGetAt(orderedKeys,temp_x)]>
                    </cfloop>
                    <cfset var = tempStruct>
                </cfif>
            </cfcase>
            
            <cfcase value="query">
                <cfif var.recordCount gt top>
                    <cfset tempQuery = queryNew(var.columnList)>
                    <cfloop index="temp_x" from=1 to="#min(var.recordCount,top)#">
                        <cfset queryAddRow(tempQuery)>
                        <cfloop index="col" list="#var.columnList#">
                            <cfset querySetCell(tempQuery,col,var[col][temp_x])>
                        </cfloop>
                    </cfloop>
                    <cfset var = tempQuery>
                </cfif>
            </cfcase>
            
        </cfswitch>
        
    </cfif>
    
    <cfdump var="#var#" expand="#expand#" label="#label#">
</cffunction>

Search CFLib.org


Latest Additions

Jose Diaz-Salcedo Jose Diaz-Salcedo added
cfRssFeed
2 day(s) ago

Raymond Compton Raymond Compton added
structBlend
23 day(s) ago

Duncan Duncan added
IsZIPUK
23 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
29 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson