PDA

View Full Version : Stack overflow in recursive function


Danne
10-16-2002, 08:55 PM
Hi!
I have a bunch of values that I'd like to store on clientside. I pass these using a big string (8kb), and then I break it in a recursive function and store it in a multidimensional array.

I have tested this function on Serverside, using ASP and JScript, and it works. But on clientside I get a stack overflow.


The structure of the string is as follows:
"[value,value,[value,value,value,value]]]"

The function that breaks this string, looks like this:


Help would be much appreciated.

function toArray(pString)
{
 var i;
 var arr = new Array();
 var ct = 0;
 var tempNode = "";
&nbsp;for (i=0; i < pString.length; i++)
&nbsp;{
&nbsp;&nbsp;if (pString.charAt(i) == "[") // Start a new level
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;pString = pString.slice(0, i+1) + pString.slice(pString.length-1);
&nbsp;&nbsp;&nbsp;arr[ct] = toArray(pString );
&nbsp;&nbsp;} else if (pString.charAt(i) == "]") // Next node
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;arr[ct] = tempNode;
&nbsp;&nbsp;&nbsp;tempNode = "";
&nbsp;&nbsp;&nbsp;ct++;
&nbsp;&nbsp;} else { // Assign value
&nbsp;&nbsp;&nbsp;tempNode += pString.charAt(i);
&nbsp;&nbsp;}
&nbsp;}
&nbsp;return arr;
}

beetle
10-16-2002, 09:36 PM
I'm about to make your life REAL easy. Javascript supports literal notation for arrays, so you just need to eval() your string.

var arr = eval(pString);

There, isn't that better? :D

I hope javascript can handle an array that big....