Skip to main content
Participating Frequently
July 25, 2006
Question

2 Dimensional Arrays - Dynamic Index Problem

  • July 25, 2006
  • 3 replies
  • 375 views
I cannot seem to get anything to work lately. Here is what I need to do -

I have a 2D array - the first index will be an integer that is sequential in order. The second index will be an integer, but it will not be sequential, and will be bascially a question number.

How do you set a 2D array using dynamic variables as the indexes?

Now here is the problem. I need to set the array in a loop. The loop is an index loop, and it is as follows - (this version WORKS)

quote:


<cfset answerArray = ArrayNew(1)>
<cfset i = 1>

<cfloop index="x" list="#form.fieldnames#">

<cfset tempstr = #x#>
<cfset newIndex = #removeChars(tempstr,1,9)#>
<cfset answerArray[#i#] = "#form#">
<cfset i = #i#+1>

<cfoutput>#form#<br></cfoutput>

</cfloop>



But this version DOES NOT - I get an http 500 error.

quote:


<cfset answerArray = ArrayNew(2)>
<cfset i = 1>

<cfloop index="x" list="#form.fieldnames#">

<cfset tempstr = #x#>
<cfset newIndex = #removeChars(tempstr,1,9)#>
<cfset answerArray[#i#] [#newIndex#] = "#form#">
<cfset i = #i#+1>

<cfoutput>#form#<br></cfoutput>

</cfloop>



And as a sidenote - How can I force my applications to give the debug information. Working at home - I get no debug info - just 500 errors, at work, I sometimes get an error message - but not all the time. the debugging info is helpful.
    This topic has been closed for replies.

    3 replies

    Participating Frequently
    July 25, 2006
    Hey guys, thanks for the advice -

    1. When I got to work and saw what the error message was saying, I had a null trying to be inserted in as an index, which of course is a NO NO. I did a quick validation check and it is now working.

    2. Thank you for pointing out the errant space. It has been taken care of as well.

    Again - thanks folks - it is nice having knowledgeable people here to help out in times of major stress.
    Inspiring
    July 25, 2006
    If this accurately shows your code, you have an illegal space.

    <cfset answerArray[#i#] [#newIndex#] = "#form#">
    ^
    Not Allowed

    <cfset answerArray[#i#][#newIndex#] = "#form#">

    PS you don't need the pounds here it could be:
    <cfset answerArray [newIndex] = form>



    Tony_Latino wrote:
    > I cannot seem to get anything to work lately. Here is what I need to do -
    >
    > I have a 2D array - the first index will be an integer that is sequential in
    > order. The second index will be an integer, but it will not be sequential, and
    > will be bascially a question number.
    >
    > Now here is the problem. I need to set the array in a loop. The loop is an
    > index loop, and it is as follows - (this version WORKS)
    >
    >
    quote:


    > <cfset answerArray = ArrayNew(1)>
    > <cfset i = 1>
    >
    > <cfloop index="x" list="#form.fieldnames#">
    >
    > <cfset tempstr = #x#>
    > <cfset newIndex = #removeChars(tempstr,1,9)#>
    > <cfset answerArray[#i#] = "#form#">
    > <cfset i = #i#+1>
    >
    > <cfoutput>#form#<br></cfoutput>
    >
    > </cfloop>
    >


    >
    > But this version DOES NOT - I get an http 500 error.
    >
    >
    quote:


    > <cfset answerArray = ArrayNew(2)>
    > <cfset i = 1>
    >
    > <cfloop index="x" list="#form.fieldnames#">
    >
    > <cfset tempstr = #x#>
    > <cfset newIndex = #removeChars(tempstr,1,9)#>
    > <cfset answerArray[#i#] [#newIndex#] = "#form#">
    > <cfset i = #i#+1>
    >
    > <cfoutput>#form#<br></cfoutput>
    >
    > </cfloop>
    >


    >
    > And as a sidenote - How can I force my applications to give the debug
    > information. Working at home - I get no debug info - just 500 errors, at work,
    > I sometimes get an error message - but not all the time. the debugging info is
    > helpful.
    >
    >
    Inspiring
    July 25, 2006
    > but it will not be sequential, and
    > will be bascially a question number.

    This suggests to me that it should not be an array, but a
    numerically-indexed struct. You can then use structKeyArray() or
    structKeyList() to pull out which indices are populated, and loop over
    them.

    --
    Adam
    Participating Frequently
    July 25, 2006
    ok, that sounds like a good suggestion, and I will try it, but why can't I get this code to work? I have indexed arrays in coldfusion before using dynamic variables - (Array[#variable1#][#variable2#]). What is so different this time?