Groups | Blog | Home
all groups > flash actionscript > may 2004 >

flash actionscript : recursive function...var gets reset


sneakyimp
5/18/2004 9:52:20 PM
i'm writing a function to recursively report the values in an array (see the
code below). the problem i'm having is that it starts into the main array just
fine, it gets into a nested array through a recursive function call...but then
when that value returns, my index in the for loop (variable "i") appears to
have been reset. it started as 11 but then was empty when the recursive call
returned. can anybody tell me what i've done wrong? here's the output:

spit out array
spitting out 'arr'
array:arr contains 11 elements.
loop i=0
number:argname.0=1
loop i=1
number:argname.1=2
loop i=2
string:argname.2=foobar mania only on sundays
loop i=3
number:argname.3=1.23456789012346e+19
loop i=4
number:argname.4=2147483647
loop i=5
number:argname.5=1.1
loop i=6
number:argname.6=2147483648
loop i=7
array:argname.7 contains 2 elements.
loop i=0
string:argname.0=first sub array
loop i=1
string:argname.1=second sub array

function SpitOutArray(arg, argname) {
var tabPrefix;
if (arguments.length == 3) {
depth = arguments[2];
for(j=0; j<depth; j++) {
tabPrefix += "\t";
}
} else {
trace("spitting out '" + argname + "'");
}
if (typeof(arg) == "object") {
intSize = arg.length;
trace(tabPrefix + "array:" + argname + " contains " + intSize + "
elements.");
depth++;
var i;
for(i=0; i<intSize; i++) {
trace("loop i=" + i);
SpitOutArray(arg[i], "argname." + String(i), depth);
}
} else if (typeof(arg) == "string") {
trace(tabPrefix + "string:" + argname + "=" + arg);
} else if (typeof(arg) == "number") {
trace(tabPrefix + "number:" + argname + "=" + arg.toString());
} else if (typeof(arg) == typeof(undefined)) {
trace(tabPrefix + 'empty element:' + argname);
}
}


// define an array--NOTICE THAT I SKIPPED INDEX 9 INTENTIONALLY
var arr = Array();
arr[0] = 1;
arr[1] = 2;
arr[2] = "foobar mania only on sundays";
arr[3] = 12345678901234567890;
arr[4] = 2147483647;
arr[5] = 1.1;
arr[6] = 2147483648;
arr[7] = new Array();
arr[7][0] = "first sub array";
arr[7][1] = "second sub array";
arr[8] = "";
arr[10] = "last item";


SpitOutArray(arr, "arr");
sneakyimp
5/18/2004 10:52:08 PM
well i have no idea really why the other function wasn't working, but instead
of a FOR loop, i have switched to a WHILE loop and i make copies of the arrays
(because arrays are passed by reference). the result here is a very useful
function for anyone who is dealing with arrays that have a complex structure
possibly with nested arrays:



function SpitOutArray(arg, argname) {
var tabPrefix;
if (arguments.length == 3) {
var depth = arguments[2];
for(j=0; j<depth; j++) {
tabPrefix += "\t";
}
} else {
trace("===========");
trace("spitting out '" + argname + "'");
}
if (typeof(arg) == "object") {
var recurse_arg = arg.slice();
var intSize = recurse_arg.length;
trace(tabPrefix + argname + " is an array containing " + intSize + "
elements.");
var index = 0;
while(recurse_arg.length) {
var element = recurse_arg.shift();
SpitOutArray(element, argname+ "." + String(index), Number(depth+1));
index++;
}
} else if (typeof(arg) == "string") {
trace(tabPrefix + argname + "=\"" + arg + "\"");
} else if (typeof(arg) == "number") {
trace(tabPrefix + argname + "=" + arg.toString());
} else if (typeof(arg) == typeof(undefined)) {
trace(tabPrefix + argname + ':<empty>');
}

if (arguments.length == 2) {
trace("===========");
trace("end of '" + argname + "'");
}

}
AddThis Social Bookmark Button