Monday, February 1, 2010

Playing with cfloop's file attribute

I was playing around with cfloop's file attribute and how it only grabs the rows you want to loop over...not the entire file. So cool. I am amazed at how well and easy it is to use. Here's what I got:

<!---data.csv--->
ID,Name,Board,Gender
1,Joe,Forum,M
2,Jamie,Nitro,F
3,Jake,Forum,M
4,Ben,Burton,M
5,Ken,Elan,M


<cfset columns = "ID,Name,Board,Gender"/>

<cfset boarders = arrayNew(1)/>

<cfloop from="2" to="6" file="C:/workspace/examples/file_looping/data.csv" index="row">

<cfset boarder = structNew()/>

<!---loop through the columns and create a struct of the row's data--->
<cfloop from="1" to="#listLen(columns)#" index="column">
<cfset boarder[listGetAt(columns,column)] = listGetAt(row,column)/>
</cfloop>

<!---append the boarder struct to the boarders array--->
<cfset arrayAppend(boarders,boarder)/>

</cfloop>

<cfdump var="#boarders#">


I put the rows in an array of structs to work with it more easily.

Use case:
It works great for imports where you want to throttle the amount of data you process at a time. Example you only want to pull records 1-99 process them, then 100-199 and so on.

No comments:

Post a Comment