I haven't tested this at all but I think it may do what you need and I
think I have accounted for potential failure points. I approached it
slightly differently and replicated the typical string.mid functions
that exist - so you provide a start point and a count and from there
it gets to your desired end point.


<cffunction name="ArrayMid" returntype="array" output="false"
hint="returns a subsection of an array">
        <cfargument name="inArray" type="array" required="true" />
        <cfargument name="startPos" type="numeric" required="true" />
        <cfargument name="count" type="numeric" required="false"
default="#ArrayLen(arguments.inArray) - arguments.startPos#" />

        <cfset var outArray = ArrayNew() />
        <cfset var i = 0 />
        <cfset var maxPosition = arrayLen(arguments.inArray) />

        <cfif MaxPosition>


                <!--- make sure the startPosition is a positive integer --->
                <cfif arguments.startPos LTE 0>
                        <cfset arguments.startPos = 1 />
                </cfif>

                <!--- make sure we aren't starting at too high of a position 
--->
                <cfif arguments.startPos GT maxPosition>
                        <cfset arguments.startPos = maxPosition />
                </cfif>

                <!--- make sure we won't go beyond the bounds of the array --->
                <cfif arguments.count + arguments.startPos GT maxPosition>
                        <cfset arguments.count = ArrayLen(arguments.inArray) - 
arguments.startPos />
                </cfif>

                <!--- get the "subArray" --->
                <cfloop from="#arguments.startPos#" to="#arguments.startPos +
arguments.count#" index="i">
                        <cfset ArrayAppend(outArray,arguments.inArray(i)) />
                </cfloop>
        </cfif>         

        <cfreturn outArray />
</cffunction>


----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to 
cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the 
email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting 
(www.cfxhosting.com).

An archive of the CFCDev list is available at 
www.mail-archive.com/cfcdev@cfczone.org


Reply via email to