0

I have two dimensional array that looks like this after I used to output this on the screen:

array
1   
array
1   0600
2   
array
1   0
3   
array
1   0600
4   
array
1   0
5   
array
1   0615
6   
array
1   0
7   
array
1   0615
8   
array
1   0
9   
array
1   0630
10  
array
1   0
11  
array
1   0630
12  
array
1   1 

So I want to loop through my array and output values in this order:

0600   0
0600   0
0615   0
0615   0
0630   0  
0630   1

Here is the code how I created my Array:

<cfloop list="#ListGetAt(dataList,i,",")#" index="z" delimiters="|">
    <cfoutput query="getR" group="theYear">
        <cfset name = myArray.append([z])>
        <cfif Description eq z>
            <cfset count = myArray.append([theCount])>
        <cfelse>
            <cfset count = myArray.append([0])>
        </cfif>
    </cfoutput>
</cfloop>

So how I can loop now to get this two records in order as I showed you above? I tried this but that did not work:

<cfoutput>
    <cfloop from="1" to="#arraylen(myArray)#" index="i">
        <cfloop array="#myArray#" index="j">
            #i# - #myArray[1][j]#<br/>
        </cfloop>
    </cfloop>
</cfoutput>

Edit: When I used this code:

<cfloop array="#myArray#" index="i"> 
    <cfloop array="#myArray#" index="j"> 
        <cfoutput> #myArray[i][j]#<br/> </cfoutput> 
    </cfloop> 
</cfloop> 

I got this error:

The value coldfusion.runtime.Array cannot be converted to a number.

If anyone can help with this problem please let me know.

4
  • I'm not a coldfusion guy but shouldn't you be indexing with [i][j] e.g. #myArray[i][j] not 1 and j Commented Mar 23, 2016 at 17:40
  • Side note, the phrase did not work: does not tell us much. While I suspect user5976242 is correct, try and include a description of the actual result, and specifically how it differs from what you expected. Commented Mar 23, 2016 at 17:49
  • When I used this code: <cfloop array="#myArray#" index="i"> <cfloop array="#myArray#" index="j"> <cfoutput> #myArray[i][j]#<br/> </cfoutput> </cfloop> </cfloop> I got this error: The value coldfusion.runtime.Array cannot be converted to a number. Commented Mar 23, 2016 at 18:01
  • @user3023588 - Okay, it makes a lot more sense now that you posted the error message ;-) If you think about the error message says exactly what the problem is... you are using an array loop - which means the index will contain the value of one of the elements in the array. However, the output code is designed for use with a from/to loop, where the index value is a position or number, ie 1,2,3,etc... Commented Mar 23, 2016 at 18:47

2 Answers 2

4

This is how you'd get the output from your 2-dimensional array.

<cfscript>
data = [
    ["0600", 0],
    ["0600", 0],
    ["0615", 0],
    ["0615", 0],
    ["0630", 0],
    ["0630", 1]
];

// script version
for (foo in data) {
    writeOutput(foo[1] & " " & foo[2] & "<br>");
}

</cfscript>

<cfoutput>
tag version...<br>
<cfloop array="#data#" index="foo">
    #foo[1]# #foo[2]#<br>
</cfloop>
</cfoutput>

An example of it in use here: http://trycf.com/gist/86b42b56ef7348ec0d44/acf2016?theme=monokai

However, I'm not sure why you're using a 2-dimensional array when an array of structs (key value pairs) seems an easier way to do it:

For example:

<cfscript>
data = [
    {key:"0600", count:0},
    {key:"0600", count:0},
    {key:"0615", count:0},
    {key:"0615", count:0},
    {key:"0630", count:0},
    {key:"0630", count:1}
];

// script version
for (foo in data) {
    writeoutput(foo.key & " " & foo.count & "<br>");
}

</cfscript>

<cfoutput>
tag version...<br>
<cfloop array="#data#" index="foo">
    #foo.key# #foo.count#<br>
</cfloop>
</cfoutput>
Sign up to request clarification or add additional context in comments.

3 Comments

How I can create array struct with key and count values from this code: <cfloop list="#ListGetAt(dataList,i,",")#" index="z" delimiters="|"> <cfoutput query="getR" group="theYear"> <cfset name = myArray.append([z])> <cfif Description eq z> <cfset count = myArray.append([theCount])> <cfelse> <cfset count = myArray.append([0])> </cfif> </cfoutput> </cfloop> ?
Something like <cfset myArray.append({name: z, count: theCount})>
I stumbled upon this looking for something else but might I saw this is a brilliant answer that can quite easily be added upon. and +1 for providing script syntax, too.
2

You can fix your loop by:

<cfoutput>
<cfloop array="#myArray#" index="firstDimension"> 
    <cfloop array="#firstDimension#" index="secondDimension"> 
        #secondDimension#<br/>
    </cfloop> 
</cfloop>
</cfoutput> 

Comments

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.