About for()

Does the ‘for’ loop, loses it’s variables and/or results, as function() does when finished? If so …

Lets say I have a function that accepts two parameters, and returns one, and I want to save the values of numorus results in variables, so logicly I create a for loop, and save it to an array. But here’s the problem, I think that the array gets lost when the for loop ends, because after the ending }; the array doesn’t exist anymore.

Or am I doing something wrong again? : /

no, not in the same way as functions (unless used in a function, then it abides by the behavior of that functions variables).

Show what you have for code and we might be able to pinpoint the problem


tMonth = new Array();
tMonth = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
tMonthDays = new Array();
tMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
mConstruct = function (d:Number, tabname:Array):Array {
	for (i = 0; i < tMonthDays[d]; i++) {
		tabname* = i + 1;
	}
	return tabname;
};

run = function () {
	for (j = 0; j < tMonth.length; j++) {
		monthName = tMonth[j];
		monthName = new Array();
		return mConstruct(j, monthName);
	}
};
a = run();
trace(a);

The part till the run(); function is done by the help of mathew.er from this http://www.kirupa.com/forum/showthread.php?t=213890 topic that I’ve created a while ago.

The code was ok, but I wanted to enchance it further, so that I wont have to write mConstruct(0, jan); mConstruct(1, feb); x12 to create arrays that I can later access. I know that I could make this far much simpler and faster, but I want to learn flexible scripting, not hardcoding data in the code.

So the code does work. What exactly is your question then? To simplify the code even further? because its already pretty modular…

The code up to run() works. Run as is doesn’t work as it supposed to. The function should create 12 arrays accessible from _root. But I can’t access them from enywhere else than the run function itself.