1

I am trying to build an array using a cfloop of query data. I am not sure how to increment the array count vs just overwriting the first value in the array each time? If this doesn't make sense please let me know.

<CFSET MyArray=ArrayNew(1)>
<CFLOOP QUERY="GetPermission">
   <CFIF #GetPermission.Permission_ID GT 10>
        <CFSET MyArray[increment value][GetPermission.Permission_ID]>
   </CFIF>
</CFLOOP>

So my array should look something like MyArray[11,14,24,25,31]

Denoteone
  • 4,043
  • 21
  • 96
  • 150

3 Answers3

4

Unless you actually need the index for something else, just omit it and use ArrayApend which "... Appends an array element to the end of a specified array".

 <cfset ArrayAppend( MyArray, GetPermission.Permission_ID)>

As an aside, when you run into a problem like this, it is helpful to peruse the functions by category section of the documentation. Most functions are very well named. Simply picking the right category, and reviewing the function names, often provides the answer right off the bat. Then it is just a matter of reading the usage docs and testing the code.

Update: However, as discussed on your other thread, ultimately there are better options than arrays for this specific task (ie update permissions list).

Community
  • 1
  • 1
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • 1
    Thank you Leigh for the assistance and recommendation. I find the Adobe ColdFusion documentation very helpful and easy to use but in this case I was not asking the correct question during my research. Thanks again! – Denoteone Mar 03 '16 at 03:59
3

For the increment you may do any of the following:

  • Use ArrayAppend() function as Leigh has mentioned
  • Set a counter variable at the top and then use that and then increment value
  • Use ArrayLen(myArray)+1 as the increment counter

Just as a side note; the script written by you has couple of issues.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Viv
  • 326
  • 1
  • 6
0

Leigh's solution is 100% accurate but, depending on the specific situation, you might not even need to dump a query's result into an array. You can access values in a query result directly.

Ben Nadel has a post about this here: http://www.bennadel.com/blog/149-ask-ben-converting-a-query-to-a-struct.htm

Steve
  • 129
  • 13