0

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.

7
  • The above does not compile. "Parameter validation error for the LISTGETAT function" Do you have the original code? Commented Jan 30, 2014 at 19:39
  • Your second loop doesn't make sense. If you are adding into myarr[1] and myarr[2], why are you getting index at [line][j] when line is a LINE of the file... not the line number. And j is looping over an arraylen of myarr, but wouldn't you want to know the length of myarr[1] or myarr[2] - Really need the original code, this is pretty messy. Commented Jan 30, 2014 at 20:29
  • 2
    Just an FYI - When looping line by line over a big file... you can just use this... it is cleaner and you dont have to worry about the delimiters etc. <cfloop file="#filepath#" index="line" > do stuff </cfloop> Commented Jan 30, 2014 at 20:32
  • 1
    I suggest baby steps. Start by declaring a 2D array and seeing if the arrayappend function works at all. It might not. Commented Jan 30, 2014 at 20:43
  • How about every time you read a row, use a ListToArray() on it? Commented Jan 30, 2014 at 21:13

1 Answer 1

1

Your logic just doesn't make any sense:

  1. Your second loop is trying to loop over the array whilst you're still building it (which is not impossible, but doesn't seem like what you want to be doing;
  2. you are looping over the length of the first dimension of the array (arraylen(myarr)), but then using that variable as an index in the second dimension of the array.
  3. I strongly suspect you want a struct as that first dimension, not an array. What is the nature of the data?

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.

Sign up to request clarification or add additional context in comments.

2 Comments

A text file of batched ACH data. I do not have the actual code to post because I ended up going in a different direction and scrapped the array code I had been working on. Thanks for the answer. I don't want to waste anyone else's time on this question.
Righto. So put an answer in & close the question, or delete it then. Don't leave it hanging around, as people will think it still needs answering. Recommend just deleting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.