Advertisement
anarchos78

The cfc

Apr 7th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <cfcomponent>
  2. <!---
  3. Splits or iterates over the array in number of groups.
  4.  
  5. @param arrObj      Array to split up in groups. (Required)
  6. @param intGroup      Number of items allowed on each group. (Required)
  7. @param padding       What should it be filled with in case there's empty slots. (Optional)
  8. @return Returns an array.
  9. @author Marcos Placona (marcos.placona@gmail.com)
  10. @version 1, February 4, 2010
  11. --->
  12. <cffunction name="arrayGroupsOf" access="public" output="false" returntype="array">
  13.     <cfargument name="arrObj" type="array" required="true" hint="An array object that will be split up in groups">
  14.     <cfargument name="intGroup" type="numeric" required="true" hint="Number of items on each group">
  15.     <cfargument name="padding" type="string" required="false" default=" " hint="What should it be filled with in case there's empty slots">
  16.    
  17.     <cfset var resArray = createObject("java", "java.util.ArrayList").Init(arguments.arrObj) />
  18.     <cfset var arrGroup = arrayNew(1) />
  19.     <cfset var arrObjGroup = arrayNew(1) />
  20.     <cfset var arrObjSize = resArray.size()>
  21.     <cfset var subStart = 0>
  22.     <cfset var subEnd = arguments.intGroup>
  23.     <cfset var ii = "">
  24.     <cfset var difference = "">
  25.     <cfset var jj = "">
  26.    
  27.     <cfset arrGroupSize = ceiling(arrObjSize / arguments.intGroup)>
  28.     <cfset arrArrayGroupSize = arrGroupSize * arguments.intGroup>
  29.    
  30.     <cfif arrArrayGroupSize GT arrObjSize>
  31.         <cfset difference = arrArrayGroupSize - arrObjSize>
  32.         <cfloop from="1" to="#difference#" index="ii">
  33.             <cfset resArray.add(arguments.padding) />
  34.         </cfloop>
  35.     </cfif>
  36.    
  37.     <cfloop from="1" to="#arrGroupSize#" index="jj">            
  38.         <cfset arrGroup = resArray.subList(subStart, subEnd)>        
  39.         <cfset arrayAppend(arrObjGroup, arrGroup)>
  40.        
  41.         <cfset subStart = subStart + arguments.intGroup>
  42.         <cfset subEnd = subEnd  + arguments.intGroup>
  43.         <cfset arrGroup = arrayNew(1) />
  44.     </cfloop>
  45.    
  46.     <cfreturn arrObjGroup>
  47.  
  48. </cffunction>
  49. </cfcomponent>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement