1
votes

i have a bunch of questions and answers, the un answered questiones must be thrown at the top with error message, the answers are in array and if un answered the array stores the value as undefined array element.

I tried to retrieve the position of the element by converting it to list, but while looping the list is missing out the empty element. when I loop it with simple loop:

  <cfloop from ="1" to="#arraylen(currentinfo.answers)#" index="ele">
  <cfset i = i+1>
   <cfif not listlen(currentinfo.answers[ele],'-')><cfset j=#i#>#j#</cfif>
  </cfloop>

I'm getting error below:

Element 5 is undefined in a Java object of type class coldfusion.runtime.Array.  

The error occurred in D:\sites\askseaton\sections\PSSurvey\Clients\tags\surveydisplay.cfm: line 194

192 : <cfloop from ="1" to="#arraylen(currentinfo.answers)#" index="ele">
193 : <cfset i = i+1>
194 :   <cfif not listlen(currentinfo.answers[ele],'-')><cfset j=#i#>#j#</cfif>
195 : </cfloop>
196 : <div>Question(s)#j# must be answered to proceed further.</div>
2
Could you try arraySort? - Sam Farmer
listLen() ignores empty list elements. Only elements with content count. - Andreas Schuldhaus

2 Answers

2
votes

Since ColdFusion 8 there has been an arrayIsDefined() function which should do what you're wanting to do.

0
votes

Or try the alternative way of looping over an array:

 <cfloop array="#currentinfo.answers#" index="ele">
  <cfset i++>
   <cfif not listlen(ele,'-')><cfset j=i>#j#</cfif>
  </cfloop>