Reputation: 6110
I'm trying to build an array that will give me all values from my list and then based on the records from the cfquery I wan to get values for years. To make this more clear I will show you below how my array should look like:
time slot year2015 year 2016
0700 0 0
0715 1 0
0730 0 0
0745 0 0
0800 0 5
and so on. My time slot values are stored in <cfset dataList>
and that looks like this:
<cfloop list="#ListGetAt(dataList,i,",")#" index="k" delimiters="|">
<cfoutput>#k#</cfoutput>
</cfloop>
0600 0615 0630 0645 0700 0715 0730 0745 0800 0815 0830 0845 0900 0915 0930 0945 1000 1015 1030 1045 1100 1115 1130 1145 1200 1215 1230 1245 1300 1315 1330 1345 1400 1415 1430 1445 1500 1515 1530 1545 1600 1615 1630 1645 1700 1800 1900 2000 2100 2200 2300 0000
My time slot, years and values for each year are outputted from cfquery that looks like this:
<cfoutput query="getRecords" group="YearRecord">
<cfoutput>
#TimeSlot# - #YearRecord# - #ValueYear#<br/>
</cfoutput>
</cfoutput>
0830 - 2015 - 2
0915 - 2015 - 2
1000 - 2015 - 1
0630 - 2016 - 1
0800 - 2016 - 1
My records are coming from two different locations. Time slots from my list and records with valid values from my cfquery. So I want to build my array that contains all records from my list and for each record I want to output year column with the valid value. So I tried first to populate my array with the elements from the list:
<cfset myArray=ArrayNew(1)>
<cfloop list="#ListGetAt(dataList,i,",")#" index="z" delimiters="|">
<cfoutput>
#myArray[z]#
</cfoutput>
</cfloop>
and this code give me an error:
Type: Expression ********** Line: 253 ********** ********** The element at position 600 of dimension 1, of array variable "MYARRAY," cannot be found.
Also after I populate array I would have to add two more elements to that array. Year and values for each year. If anyone knows what I'm doing wrong and what is the best way to do this please let me know. Thanks in advance.
Upvotes: 0
Views: 1196
Reputation: 5812
When you want to add something to array use arrayAppend
. Remember that's a function, so please it:
arrayAppend(myArray, thingsToAdd);
Only on Lucee or in CF2016 it works with manner:
myArray.arrayAppend(thingToAdd);
Upvotes: 1
Reputation: 1621
At begining. Don't know Why everywhere You are using ListGetAt(dataList,i,","). Is that inside bigger loop?
This code
<cfset myArray=ArrayNew(1)>
<cfloop list="#ListGetAt(dataList,i,",")#" index="z" delimiters="|">
<cfoutput>
#myArray[z]#
</cfoutput>
</cfloop>
is throwing error because myArray is empty when you are looping over your list. If you want to fill it that way you shuld do it like that
<cfset myArray=ArrayNew(1)>
<cfset yourList = ListGetAt(dataList,i,",")>
<cfloop list="#yourList#" index="z" delimiters="|">
<cfset myArray.append(z)>
</cfloop>
or simply
<cfset yourList = ListGetAt(dataList,i,",")>
<cfset myArray = ListToArray(yourList)>
But if i understand it well you need 2 dimensional array. You should do it like that.
<cfset myArray=ArrayNew(2)>
<cfset yourList = ListGetAt(dataList,i,",")>
<cfloop list="#yourList#" index="z" delimiters="|">
<!--- we will append array with our value. later you can append to this year and other values from query --->
<cfset myArray.append([z])>
</cfloop>
EDIT 1 Maybe you should think about Struct not about array? Create struct with all your time slots as key. I will use short notation.
<cfset str = {}>
<cfloop list="#yourList#" index="z" delimiters="|">
<cfset str[z] = {'y2015'=0,'y2016'=0,'valYear'=0}>
</cfloop>
<cfoutput query="getR" group="theYear">
<cfif theYear eq 2015>
<cfset str[z]['y2015'] = theCount>
<cfelse>
<cfset str[z]['y2016'] = theCount>
</cfif>
</cfoutput>
<cfdump var="#str#">
Now you can use StructKeyList to get List of key from that stuct, to loop over that struct.
Upvotes: 1