djlurch
asked on
Multi-Dimensional Array Help: Year/Month
I am having some problems understanding and coding multi-dimensional arrays. I am pretty sure this is what I need to use.
Data structure required:
Array of Years (2000-2015)
Array of Month (1-12)
I want to iterate through a recordset and store a sum of values for each month/year (example: March 2012).
Can someone help with some code to get me started?
Data structure required:
Array of Years (2000-2015)
Array of Month (1-12)
I want to iterate through a recordset and store a sum of values for each month/year (example: March 2012).
Can someone help with some code to get me started?
ASKER
Thank you for the answer. However the syntax and structure of Javascript and VbScript different and it is not a trivial task to translate the code.
I am still looking for an answer.
I am still looking for an answer.
'Array of Years (2000-2015)
'Array of Month (1-12)
strYears="2000-2015" ' assumeing this is the actual way we get the data'
strMonth="1-12"
arrYears=split(strYears,"-") ' creates the array
arryMonth=split(strMonth,"-")
minYear=arrYears(0)' arrays are zero based
maxYear=arrYears(1)
minMonth=arrMonth(0)
maxMonth=arrMonth(1)
For y = minYear to maxYear
for m = minMonth to maxMonth
response.write MonthName(m) & " "& y&"<br>"
' do some magic here
next
next
Lets improve on that and make a dynamic array to store the data you want to calculate.
'Array of Years (2000-2015)
'Array of Month (1-12)
dim YearMonth() ' this will be are dynamci array
dim counter
counter= -1
strYears="2000-2015" ' assumeing this is the actual way we get the data'
strMonth="1-12"
arrYears=split(strYears,"-") ' creates the array
arryMonth=split(strMonth,"-")
minYear=arrYears(0)' arrays are zero based
maxYear=arrYears(1)
minMonth=arrMonth(0)
maxMonth=arrMonth(1)
For y = minYear to maxYear
for m = minMonth to maxMonth
counter=counter+1
response.write MonthName(m) & " "& y&"<br>"
' do some magic here
if counter=0 then
ReDim YearMonth(0)
else
ReDim Preserve YearMonth(counter) 'Preserve keeps the data intact
end if
YearMonth(counter)=MonthName(m) & " "& y
next
next
response.write "<hr>Now lets see what is in our dynamic array"
for each x in YearMonth
response.write x &"<br>"
next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Padas!
Your welcome! thanks for the points. I use this similar procedure a lot.
var rArray=new array();
for (var i=2005;i<2016;i++)
{
for (var j=1;J<13;j++)
rArray(rArray.length)=new Array(i,j)
}
At the end of this procedure you should have a two dimensional array with all the months from all the years. It should be easy enough to convert this sample into any other specific language you may be using.
MrC.