Skip to main content
Participating Frequently
June 5, 2011
Question

CF9 Structures and StructFindValue

  • June 5, 2011
  • 2 replies
  • 1351 views

I'm working on a multipage survey, and I'd like to store the answers in a structure.

First I create an array:

<cfset answerarray = arrayNew(1)>

Then I create a structure, and I loop through the questions in the survey and store the data.

<cfset ArrayPosition=1>

<cfset answerarray[ArrayPosition] = structNew()>
<cfset answerarray[ArrayPosition].CategoryID=CategoryID>
<cfset answerarray[ArrayPosition].QuestionID=QuestionID>
<cfset answerarray[ArrayPosition].AnswerValue=AnswerValue>

Then I convert the structure data to WDDX.

<CFWDDX action="CFML2WDDX" input="#answerarray#" output="wddx_answerarray">

And I store it in a hidden field.

<form action="test4.cfm" method="post">
  <cfoutput>
   <input type="hidden" name="wddx_answerarray" value="#wddx_answerarray#">
  </cfoutput>
  <input type="submit">
</form>

That all works great, and on the next page, I convert the data back to a structure.

<cfif IsDefined("wddx_answerarray")>
    <CFWDDX action="WDDX2CFML" input="#wddx_answerarray#" output="answerarray">
</cfif>

If I do a cfdump at this point, I get something like this:

array
1
struct
ANSWERVALUEYes
CATEGORYID1
QUESTIONID1
2
struct
ANSWERVALUENo
CATEGORYID1
QUESTIONID2
3
struct
ANSWERVALUEMaybe
CATEGORYID1
QUESTIONID3
4
struct
ANSWERVALUEYes
CATEGORYID2
QUESTIONID1

So what I'd like to do is check to see if there is a specific categoryID stored in the structure, so I did the following:

<cfset match=StructFindValue(answerarray,"1","all")>
<cfdump var="#match#">

But I get the error:

You have attempted to dereference a scalar variable of type class java.util.Vector as a structure with members.

I've got no idea what I'm doing wrong.  Any pointers would be greatly appreciated.

Thanks,

Jim

    This topic has been closed for replies.

    2 replies

    Participating Frequently
    June 5, 2011

    Ok, I've come up with this:

    <cfloop index="ArrayPosition" from="1" to="#ArrayLen(answerarray)#">
        <cfif answerarray[ArrayPosition].CategoryID EQ CurrentCategoryID>
            <cfoutput>
            #answerarray[ArrayPosition].CategoryID# - #answerarray[ArrayPosition].QuestionID# - #answerarray[ArrayPosition].AnswerValue#<br/>
            </cfoutput>
        </cfif>
    </cfloop>

    This displays the information I want.  It might be a bit clunky, since I have to loop through all the data to find the specific category I'm looking for, but it seems to work.

    Thanks to everyone who took the time to offer suggestions.

    Inspiring
    June 5, 2011

    <cfset match=StructFindValue(answerarray,"1","all")>
    <cfdump var="#match#">

    answerarry is... um... an array.  Not a struct.

    --

    Adam

    Participating Frequently
    June 5, 2011

    Well, it started as an array, hence the name.  It seemed to work better as an array of structures, and if you look at the cfdump, you'll see that it is..  Am I creating this structure incorrectly?  If I try #ArrayFind(answerarray,"yes")# I get 0, so it doesn't look like I can search the array that way either.

    Thanks.

    Inspiring
    June 5, 2011

    It started that way, and it still is.  It might be an array of structs, but it's still an array.  And you can't give structFindValue() an array as an argument.  You need to give it a struct.

    --

    Adam