Well. it is possible (anything's possible!) but I'm not sure why you'd you
want to.

Remember that structures are inherently without order - there really isn't a
stable concept of "the first ten elements" when it comes to structs.  In CF
Structs _tend_ to maintain order but there's no guarantee of that in this or
future versions.  Adding elements to a struct could change the physical
order of the elements (structs are, in theory, stored to save space and make
random access easier, not to maintain order).

If you want to page through things like this then an array is really much
more appropriate.

All that said however remember that a struct can be accessed using array
notation.  For example if you had a struct key accessible with
#myStruct.myKey# you could also access it with #myStruct["myKey"]#.  The key
here is that in the second example the key name could also be variable or
expression.  This means that this also works:

<cfset myVar = "myKey">
<cfoutput>#myStruct[myVar]#</cfoutput>

Using that you could handle your problem in several ways:

1) If the keys can be renamed you could name all your keys using integers.
(To name a key using a non-standard variable name use indexed notation for
the assignment as in <cfset myStruct["1"] = value> ).  You can then loop
over the length of the collection and use the count as the accessor.  Of
course if you could do this then you could probably also just use an array
to begin with.

2) As somebody else noted you could get a KeyListArray from the struct.  You
can then loop over that array and use the values to access the struct.
Something like (this assumes that "myStruct" is already populated with
data):

        <!--- Get the KeyList --->
<cfset myKeys = structKeyArray(myStruct)>
        <!--- Get the length of the struct (as the upper count) --->
<cfset myLength = ArrayLen(myKeys)>
<cfloop from="1" to="#myLength#" index="Cnt">
        <cfoutput>#myStruct[myKeys[Cnt]]#</cfoutput>
</cfloop>

Remember, of course, that your loop counter doesn't have to start at one!
This is how you'd get your "pages" (I may be off here - doing all this on
the fly):

        <!--- Set the Current Page --->
<cfset CurrentPage = 3>
        <!--- Get the KeyList --->
<cfset myKeys = structKeyArray(myStruct)>
        <!--- Get the Starting Count --->
<cfset CurrentBegin = CurrentPage * 10>
        <!--- Get the Ending Count (the "if" determines if we're at the end
of the set) --->
<cfif CurrentBegin + 9 GT ArrayLen(myKeys)>
        <cfset CurrentEnd = ArrayLen(myKeys)>
<cfelse>
        <cfset CurrentEnd = CurrentBegin + 9>
</cfif>
        <!--- Loop over the content and great just the "current page" --->
<cfloop from="#CurrentBegin#" to="#CurrentEnd#" index="Cnt">
        <cfoutput>#myStruct[myKeys[Cnt]]#</cfoutput>
</cfloop>


There are definitely other ways to get this done, but those should
get'r'done.

Jim Davis



----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to 
[email protected] 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/[email protected]


Reply via email to