CFLib.org – Common Function Library Project

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

Last updated October 16, 2002

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

 
Rated 2 time(s). Average Rating: 4.5

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

Return Values:
Returns a string.

Example:

view plain print about
<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:

view plain print about
<!---
 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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
savecontent
24 day(s) ago

Pete Ruckelshaus Pete Ruckelshaus added
firstDayOfWeek
a while ago

Chris Weller Chris Weller added
convertQueryStri...
a while ago

Dan Bracuk Dan Bracuk added
getExcelPercenti...
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 42 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 8 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson