Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to set up a 3D Array...what am i doing wrong??? 1

Status
Not open for further replies.

GregLanders

Programmer
Jul 9, 2001
17
US
I am trying to set up an array with 3 dimensions and then test it by outputing it with 3 nested loops. Here is my code:


<!--- Initializing an array with 3 dimensions --->
<cfset menuitems = ArrayNew(3)>

<!--- Defining the elements of the array --->
<cfset menuitems[1] = &quot;Fruit,/fruit/index.html&quot;>
<cfset menuitems[1][1] = &quot;Apples,/fruit/apples.html&quot;>
<cfset menuitems[1][1][1] = &quot;Green,/fruit/green_apples.html&quot;>
<cfset menuitems[1][1][2] = &quot;Red,/fruit/red_apples.html&quot;>
<cfset menuitems[1][2] = &quot;Oranges,/fruit/oranges.html&quot;>
<cfset menuitems[1][2][1] = &quot;Little Oranges,/fruit/little_oranges.html&quot;>
<cfset menuitems[1][2][2] = &quot;Big Oranges,/fruit/big_oranges.html&quot;>
<cfset menuitems[1][2][3] = &quot;Round Oranges,/fruit/round_oranges.html&quot;>
<cfset menuitems[1][2][4] = &quot;Navel Oranges,/fruit/navel_oranges.html&quot;>
<cfset menuitems[1][2][5] = &quot;Seedless Oranges,/fruit/seedless_oranges.html&quot;>
<cfset menuitems[1][3] = &quot;Bannanas,/fruit/bannanas.html&quot;>
<cfset menuitems[1][4] = &quot;Exotic Fruits,/fruit/exotic_fruits.html&quot;>
<cfset menuitems[1][4][1] = &quot;Kiwi,/fruit/kiwi.html&quot;>
<cfset menuitems[1][4][2] = &quot;Pineapple,/fruit/pineapple.html&quot;>
<cfset menuitems[1][4][3] = &quot;Mango,/fruit/mango.html&quot;>

<cfset menuitems[2] = &quot;Vegetables,/vegetables/index.html&quot;>
<cfset menuitems[2][1] = &quot;Corn,/vegetables/corn.html&quot;>
<cfset menuitems[2][1][1] = &quot;Popcorn,/vegetables/popcorn.html&quot;>
<cfset menuitems[2][1][2] = &quot;Yellow Corn,/vegetables/yella_corn.html&quot;>
<cfset menuitems[2][2] = &quot;Carrots,/vegetables/carrots.html&quot;>
<cfset menuitems[2][3] = &quot;Peas,/vegetables/peas.html&quot;>

<!--- Testing the array with a set of nested loops --->
<cfloop index = &quot;loop1&quot; from=&quot;1&quot; to = &quot;#ArrayLen(menuitems)#&quot;>
<cfoutput>
[#loop1#][#loop2#][#loop3#]</b>: #menuitems[loop1]#<br>
</cfoutput>
<cfloop index = &quot;loop2&quot; from=&quot;1&quot; to = &quot;#ArrayLen(menuitems[loop1])#&quot;>
<cfoutput>
[#loop1#][#loop2#][#loop3#]</b>: #menuitems[loop1][loop2]#<br>
</cfoutput>
<cfloop index = &quot;loop3&quot; from=&quot;1&quot; to = &quot;#ArrayLen(menuitems[loop1][loop2])#&quot;>
<cfoutput>
[#loop1#][#loop2#][#loop3#]</b>: #menuitems[loop1][loop2][loop3]#<br>
</cfoutput>
</cfloop>
</cfloop>
</cfloop>


Here is the error that am getting:

<HR><H3>Error Occurred While Processing Request</H3><P> <TABLE BORDER><TR><TD><H4>Error Diagnostic Information</H4><P><P>An error occurred while evaluating the expression:
<P><PRE> menuitems[1] = &quot;Fruit,/fruit/index.html&quot;
</PRE></P></P>Error near line 12, column 16.<HR><P>Cannot set element of indexed object<P><p>The element at position 1 of the object cannot be set. May be the object is read-only. The object has elements in positions 1 through 0.<P>The error is in dimension 1 of object &quot;menuitems&quot;.<P> <p>The error occurred while processing an element with a general identifier of (CFSET), occupying document position (12:1) to (12:48).</p><P><P>Date/Time: 07/15/01 11:04:32<BR>Browser: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; MSOCD)<BR>Remote Address: 24.249.61.22<P></TD></TR></TABLE><P><HR>
 
im not sure, but i hope this to help you..

everytime you asign a value to the array you have to specify every of the dimensions.

An array of three dimensions is like a cube... why are you using a 3-D array? (they are not very useful/common)


Anyway, the first element of your array might be defined like this:


<!--- Defining the elements of the array --->
<cfset menuitems[0][0][0] = &quot;Fruit,/fruit/index.html&quot;>

the second:

<cfset menuitems[0][0][1] = &quot;Apples,/fruit/apples.html&quot;>


and so on, depending on which are your dimensions and the place you want to use.


I think is not very useful to use a 3d array... i suggest use to change it for a 2d. it's easier to control and to set. it is just as an excell table, to say a graphic example... and a 3d is exactly as a cube, every of the faces is a space on the array...
_____
_/_/_/|
|_|_|_|/

I hope this helps you

 
I have messed around with this all weekend and have finally achieved the results I have been looking for. Thank you very much for your reply. Here is my final code:
happy.gif


Code:
<!--- Initializing an array with 3 dimensions --->
<cfset menuitems = ArrayNew(3)>
<!--- Defining the array elements of the menu --->
<cfset menuitems[1][1][1] = &quot;Fruit,/fruit/index.html&quot;>
	<cfset menuitems[1][2][1] = &quot;Apples,/fruit/apples.html&quot;>
		<cfset menuitems[1][2][2] = &quot;Green,/fruit/green_apples.html&quot;>
		<cfset menuitems[1][2][3] = &quot;Red,/fruit/red_apples.html&quot;>
	<cfset menuitems[1][3][1] = &quot;Oranges,/fruit/oranges.html&quot;>
		<cfset menuitems[1][3][2] = &quot;Little Oranges,/fruit/little_oranges.html&quot;>
		<cfset menuitems[1][3][3] = &quot;Big Oranges,/fruit/big_oranges.html&quot;>
		<cfset menuitems[1][3][4] = &quot;Round Oranges,/fruit/round_oranges.html&quot;>
		<cfset menuitems[1][3][5] = &quot;Navel Oranges,/fruit/navel_oranges.html&quot;>
		<cfset menuitems[1][3][6] = &quot;Seedless Oranges,/fruit/seedless_oranges.html&quot;>
	<cfset menuitems[1][4][1] = &quot;Bannanas,/fruit/bannanas.html&quot;>
	<cfset menuitems[1][5][1] = &quot;Exotic Fruits,/fruit/exotic_fruits.html&quot;>
		<cfset menuitems[1][5][2] = &quot;Kiwi,/fruit/kiwi.html&quot;>
		<cfset menuitems[1][5][3] = &quot;Pineapple,/fruit/pineapple.html&quot;>
		<cfset menuitems[1][5][4] = &quot;Mango,/fruit/mango.html&quot;>

<cfset menuitems[2][1][1] = &quot;Vegetables,/vegetables/index.html&quot;>
	<cfset menuitems[2][2][1] = &quot;Corn,/vegetables/corn.html&quot;>
		<cfset menuitems[2][2][2] = &quot;Popcorn,/vegetables/popcorn.html&quot;>
		<cfset menuitems[2][2][3] = &quot;Yellow Corn,/vegetables/yella_corn.html&quot;>
	<cfset menuitems[2][3][1] = &quot;Carrots,/vegetables/carrots.html&quot;>
	<cfset menuitems[2][4][1] = &quot;Peas,/vegetables/peas.html&quot;

<cfoutput>

<!--- Three loops that extract the menu data and construct the JavaScript objects --->
<cfloop index = &quot;loop1&quot; from=&quot;1&quot; to = &quot;#ArrayLen(menuitems)#&quot;>
	<cfloop index = &quot;loop2&quot; from=&quot;1&quot; to = &quot;#ArrayLen(menuitems[loop1])#&quot;>
		<cfloop index = &quot;loop3&quot; from=&quot;1&quot; to = &quot;#ArrayLen(menuitems[loop1][loop2])#&quot;>
			<!--- Check to see if we are looking a TOP level item --->
			<cfif (#loop2# is 1) and (#loop2# is 1)>
				<!--- Determine the TOP level number --->
				<cfset variables.topnum = #loop1# - 1>
				<!--- Construct the JavaScript object name --->
				<cfset variables.top = &quot;top#variables.topnum#&quot;>
				<!--- Parse the element's list value for the label and url --->
				<cfset variables.label = #ListGetAt(menuitems[loop1][loop2][loop3],1)#>
				<cfset variables.url = #ListGetAt(menuitems[loop1][loop2][loop3],2)#>
				<!--- Output the JavaScript line that declares the value of a TOP level object --->
				GL.makeMenu(&quot;#variables.top#&quot;,&quot;&quot;,&quot;#variables.label#&quot;,&quot;#variables.url#&quot;);
			</cfif>
			<!--- Check to see if we are looking a SUB level item --->
			<cfif (#loop2# GTE 2) and (#loop3# EQ 1)>
				<!--- Determe the SUB level number --->
				<cfset variables.subnum = #loop2# - 2>
				<!--- Construct the JavaScript object name --->
				<cfset variables.sub1 = &quot;sub#variables.topnum##variables.subnum#&quot;>
				<!--- Parse the element's list value for the label and url --->
				<cfset variables.label = #ListGetAt(menuitems[loop1][loop2][loop3],1)#>
				<cfset variables.url = #ListGetAt(menuitems[loop1][loop2][loop3],2)#>
				<!--- Output the JavaScript line that declares the value of a SUB level object --->
				GL.makeMenu(&quot;#variables.sub1#&quot;,&quot;#variables.top#&quot;,&quot;#variables.label#&quot;,&quot;#variables.url#&quot;);
			</cfif>
			<!--- Check to see if we are looking a SUB-SUB level item --->
			<cfif (#loop2# GTE 2) and (#loop3# GTE 2)>
				<!--- Determine the SUBSUB level number --->
				<cfset variables.subsubnum = #loop3# - 2>
				<!--- Construct the JavaScript object name --->
				<cfset variables.sub2 = &quot;sub#variables.topnum##variables.subnum##variables.subsubnum#&quot;>
				<!--- Parse the element's list value for the label and url --->
				<cfset variables.label = #ListGetAt(menuitems[loop1][loop2][loop3],1)#>
				<cfset variables.url = #ListGetAt(menuitems[loop1][loop2][loop3],2)#>
				<!--- Output the JavaScript line that declares the value of a SUB-SUB level object --->
				GL.makeMenu(&quot;#variables.sub2#&quot;,&quot;#variables.sub1#&quot;,&quot;#variables.label#&quot;,&quot;#variables.url#&quot;);
			</cfif>
		</cfloop>
	</cfloop>
</cfloop>

</cfoutput>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top