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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't Display Array

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

I'm having trouble displaying an arrray I've created from 2 queries. Basically, I did a query on a table, and used the results to get data from another query on a table. Here's the pseudocode:

<create Array &quot;fullArray&quot;>
<Query 1>

<loop through Query 1>
<Query 2> ...where items from Q1 match

<if Q2.recordcount neq 0>
populate fullArray
<end if>
<end of loop>

Output contents of fullArray.


The problem is when I try to output the contents of the array, I receive the following error:

The element at position 1 in dimension 2 ob object &quot;fullArray&quot; cannot be found. That dimension of the object is empty. Please modify the index expression.&quot;


But I know at least one record was found to populate the array. And when I do #ArrayLen#, I get a number.

Here's my code. Please note the SQL where clause. The funny thing is if I just scale down the where clause to pull 2 records, the code works just fine.

<cfset fullArray = ArrayNew(2)>

<!--- Populate fullArray --->
<CFLOOP FROM=&quot;1&quot; TO=&quot;#POLDATA.recordcount#&quot; index=&quot;P1&quot;>
<cfquery name=&quot;GETEND&quot; datasource=&quot;#session.dsn#&quot;>
select wend_policy_alpha,
wend_policy_number,
wend_renewal_seq_number,
wend_endorsement_no,
wend_end_code_1,
wend_end_code_2,
wend_end_code_3,
wend_end_code_4,
wend_end_code_5,
wend_end_code_6,
wend_end_code_7,
wend_end_code_8,
wend_end_code_9,
wend_end_code_10
from PLWEBEND
where (wend_policy_alpha = '#POLDATA.wpol_policy_alpha[P1]#' and wend_policy_number = #POLDATA.wpol_policy_number[P1]# and wend_renewal_seq_number = #POLDATA.wpol_renewal_seq_number[P1]# and wend_endorsement_no = #POLDATA.wpol_endorsement_no[P1]#)

</cfquery>
<!--- Only populate array if a record was found in 2nd query --->
<cfif #GETEND.recordcount# neq 0>
<cfset fullArray[P1][1] = &quot;#POLDATA.wpol_ins_name[P1]#&quot;>
<cfset fullArray[P1][2] = #POLDATA.wpol_endorse_date[P1]#>
<cfset fullArray[P1][3] = #POLDATA.wpol_activity_date[P1]#>
<cfset fullArray[P1][4] = &quot;#POLDATA.wpol_agent_code[P1]#&quot;>
<cfset fullArray[P1][5] = &quot;#POLDATA.wpol_policy_alpha[P1]##POLDATA.wpol_policy_number[P1]#&quot;>
<cfset fullArray[P1][6] = Trim(&quot;#GETEND.wend_end_code_1##GETEND.wend_end_code_2##GETEND.wend_end_code_3##GETEND.wend_end_code_4##GETEND.wend_end_code_5##GETEND.wend_end_code_6##GETEND.wend_end_code_7##GETEND.wend_end_code_8##GETEND.wend_end_code_9##GETEND.wend_end_code_10#&quot;)>
</cfif>

<cfoutput>#GETEND.recordcount#</cfoutput>.<br>

</CFLOOP>

Number of records in fullArray is <cfoutput>#ArrayLen(fullArray)#</cfoutput>.<br>
Show populated array (fullArray). <br>

<cfloop from=&quot;1&quot; to=&quot;#ArrayLen(fullArray)#&quot; index=&quot;a1&quot;>
<cfloop from=&quot;1&quot; to=&quot;6&quot; index=&quot;a2&quot;>
<cfoutput>#fullArray[a1][a2]# | </cfoutput>
</cfloop> <!--- array output --->
<hr><br>
</cfloop>

Please help if you can.

Thanks in advance,
scripter73

Change Your Thinking, Change Your Life.
 
My guess is that the 2nd loop through the query doesn't have any records, so dimension 2 of the array is never created:

[COLOR=666666]<!--- Only populate array if a record was found in 2nd query --->[/color]
<cfif #GETEND.recordcount# neq 0>

The array still exists, just with no 2nd dimension. To troublshoot this I would suggest dumping the variables:

ColdFusion 5.x - <cfdump> tag
=== START CODE EXAMPLE ===
<cfdump var=&quot;#fullArray#&quot;>
=== END CODE EXAMPLE ===

ColdFusion 4.x - <CF_ObjDump> Custom Tag
=== START CODE EXAMPLE ===
<CF_ObjDump object=&quot;#fullArray#&quot;>
=== END CODE EXAMPLE ===

<CF_ObjDump>: Example and Download
- tleish
 
Hi tleish,

Thanks for your help. I'm running CF 4.5, so I downloaded the tag contents into my directory, and I put the following in my code right after the population of fullArray:


Number of records in fullArray is <cfoutput>#ArrayLen(fullArray)#</cfoutput>.<br>
<!--- Show populated array (fullArray). <br>

<cfloop from=&quot;1&quot; to=&quot;#ArrayLen(fullArray)#&quot; index=&quot;a1&quot;>
<cfloop from=1 to=6 index=&quot;a2&quot;>
<cfoutput>#fullArray[a1][a2]# | </cfoutput>
</cfloop> <!--- array output --->
<hr><br>
</cfloop> --->

<CF_ObjDump> Custom Tag
=== START CODE EXAMPLE ===<br>
<CF_ObjDump object=&quot;#fullArray#&quot;>
=== END CODE EXAMPLE ===<br>



And I got this error message:

Error!
The object is not a valid object. Make sure you specify a valid ColdFusion object type (structure, query, array, variable, etc.) Also, make sure you are not evaluating the contents of the object, but are providing CF_ObjDump with the name of your object. (i.e. use &quot;SESSION&quot;, not &quot;#SESSION#&quot;.)

Custom Tag === START CODE EXAMPLE ===

Error!
The object



Did I put it in my code incorrectly? I'm not getting a dump?

Thanks for your help,
scripter73

Change Your Thinking, Change Your Life.
 
tleish,

I was able to get it to work. Here's what it has:

fullArray[1] This array contains no elements.
fullArray[2] fullArray[2][1] ABEL NON
fullArray[2][2] 6062002
fullArray[2][3] 7162002
fullArray[2][4] A90135
fullArray[2][5] CCTUPA1
fullArray[2][6] AVACAL


But there's only supposed to be 1 record in my array, because only one record matches the recordcount neq 0. How come I get 2? Do you see anything wrong with my code?

Thanks,
scripter73
P.S. Thanks for the fab custom tag!!!



Change Your Thinking, Change Your Life.
 
tleish,

I discovered what the problem is. Duh!

Its in the following code:


<cfif #GETEND.recordcount# neq 0>
<cfset fullArray[P1][1] = &quot;#POLDATA.wpol_ins_name[P1]#&quot;>
<cfset fullArray[P1][2] = #POLDATA.wpol_endorse_date[P1]#>
<cfset fullArray[P1][3] = #POLDATA.wpol_activity_date[P1]#>
<cfset fullArray[P1][4] = &quot;#POLDATA.wpol_agent_code[P1]#&quot;>
<cfset fullArray[P1][5] = &quot;#POLDATA.wpol_policy_alpha[P1]##POLDATA.wpol_policy_number[P1]#&quot;>
<cfset fullArray[P1][6] = Trim(&quot;#GETEND.wend_end_code_1##GETEND.wend_end_code_2##GETEND.wend_end_code_3##GETEND.wend_end_code_4##GETEND.wend_end_code_5##GETEND.wend_end_code_6##GETEND.wend_end_code_7##GETEND.wend_end_code_8##GETEND.wend_end_code_9##GETEND.wend_end_code_10#&quot;)>
</cfif>


I'm using the same variable I use to loop, i.e., P1, to create my array elements. So the first time through the loop, there's no match, and the second time there is, so I attempt to say <cfset fullArray[2][1] = value>. That's how I get a value in the 2nd record but not the first.

The solution is to say something like:

<cfset a = #ArrayLen(fullArray)#>
<cfif loop>

...populate fullArray[a+1] = value

</cfif>

Thanks for your help.
scripter73
Change Your Thinking, Change Your Life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top