Reputation: 5
I was working with a 2D array this past week and no matter what I tried, I was not able to access the data elements in the array. Coldfusion kept returning "Complex/simple value" errors or index of elemnet in position 1, etc..
I moved on a found a different method using a struct, but I am curious as to why I could not get the correct index.
I was trying to read in a text file:
<cfset myarr = arraynew(2) />
<cffile action="read" file="#filepath#" variable="filedata" />
<cfloop list="#filedata#" index="line" delimiters="#chr(13)##chr(10)#">
<cfset line = trim( line ) />
<cfif line contains "routing number">
<cfset arrayappend( myarr[1], listlast( line, ":" )) />
<cfelseif line contains "account number">
<cfset arrayappend( myarr[2], listlast( line, ":" )) />
</cfif>
<cfloop index="j" from="1" to="#arraylen( myarr )#" step="1">
<cfoutput>
#listgetat( myarr[line][j] )#
</cfoutput>
</cfloop>
</cfloop>
Now, if I dump out my array, the array looks correct
array
1
1 999999999
2 111111111
array
2
1 12345678
2 987654321
However, the nested loop above does not get the correct position of the element in the index and I do not understand why.
Thanks for any help or insight you can provide.
Upvotes: 0
Views: 916
Reputation: 29870
Your logic just doesn't make any sense:
arraylen(myarr)
), but then using that variable as an index in the second dimension of the array.Also, as someone else alluded to, pls post your actual code. That cannot be your code as it doesn't even compile, let alone run.
This answer doesn't get you to where you want to end up (because you haven't really defined that clearly), but it explains why you're definitely not getting there.
Can I suggest you revise your question to describe what you actually want to achieve, and revise your code in accordance with all the suggestions in the comments first. And then post code which actually compiles.
Upvotes: 1