Array inside a function

Hi everyone,

I am having a hard time trying to get this to work.

On my main timeline, I create a new array called aEventData:

aEventData = new Array();

On the next frame, I loop through data pulled from ColdFusion. I am trying to put the data into an array so that I can reference it later based on it’s position. My function to do this looks like so:

function eventDataQuery_Result ( result ){
// for each record in the recordset…
for (a=0;a<result.getLength();a++){
// Use the record variable to refer to the current row of the recordset
var record = result.getItemAt(a);
//Fill our array with data from the event
_root[aEventData[record.eventDate]] = record.eventDesc;
};
}

This doesn’t work. I don’t know how to properly reference the array on the timeline. I also tried:
_root[(aEventData[record.eventDate])] = record.eventDesc;
and
_root.aEventData[record.eventDate] = record.eventDesc;
(I knew this one wouldn’t work but I had to try.)

If I use aEventData[record.eventDate] = record.eventDesc;
and then trace the value from within my function, it works okay, but it doesn’t set the value in the array on my main timeline. Can anyone explain how I can reference and set values in my array from within the function?

Thanks.

Try


this.aEventData[record.eventDate] = record.eventDesc

I used this because aEventData was declared on the main timeline, and since this frame is on the main timeline as well we can use this to target it.

Also, make sure that record.eventDate is a number, otherwise it won’t work. You could also add trace actions to debug. Like for example tracing the array to see if it is being targetted correctly. And, tracing record.eventDate to see if it’s a correct number, and different from every other number. If not, you’re overwriting existing values in the array.

So use:


function eventDataQuery_Result ( result ){
for (a=0;a<result.getLength();a++){
var record = result.getItemAt(a);
trace("record.eventData = "+record.eventDate);
trace("Array = "+this.aEventData);
_root[aEventData[record.eventDate]] = record.eventDesc;
};
}

and let us know what that outputs.

I got it.

I used the traces that you suggested and my output was coming out as it was supposed too. Just out of curiosity, I tried putting an trace(aEventData.length) on the third frame, and it worked.
I ran some more testes by putting some ytrace statements directly after the function. Any time I traced it here, it wouldn’t work - it looked like the code after the function was running BEFORE the actual function itself, which is why it5 looked to me like it wasn’t setting the data.

Anyway, not sure why the code runs this way I would have thought it ran sequentially through the lines, but in any case I got it workinh now. Thanks for the help!

Well the code you showed only declares the function; nothing happens yet. You must execute it for it to do it’s job. The function is first defined, and then it can be called. That’s why the code after the function declaration will be executed ‘earlier’ if you don’t execute it, or execute it later than you execute the function.

Good you got it working :slight_smile: