...

View Full Version : problem with local/global arrays



lolena1888
07-14-2004, 02:48 PM
hi!

I wrote 2 functions. First declares an array and calls function brakeInto
The second function makes myArray 2 dimensional. Why I can not see the elements stored in that array after working the second function?

function func1(mystring)
{ myArray=new Array();

brakeInto(mystring,0);

// The problem begins here!!!!!!!!!!!!!!!!!!!!
//It tells me that myArray is undefined!
for (i=1;i<7;i++)
{
for (j=1;j<4;j++)
{alert(myArray[i][j]);}
}
return myArray;
}
function brakeInto(msg, flag)
{ var counter=0;
var brakeline;
var str;

if(!flag)
{ brakeline=String.fromCharCode(13)+String.fromCharCode(10);}
else
{ brakeline=String.fromCharCode(009);
myArray[flag]=new Array();
}

var prevResult=0;
var theResult = msg.indexOf(brakeline);

while (theResult != -1)
{ counter++;
str=msg.substring(prevResult,theResult);
if(!flag)
{ brakeInto(str, counter);}
else
{ myArray[flag][counter]=str;}
prevResult=theResult;
theResult= msg.indexOf(brakeline, theResult + 1); // start search after the last match found
}
counter++; //count the last element
str=msg.substring(prevResult,msg.length); //save the last element
if (!flag)
{ brakeInto(str, counter);}
else
{ myArray[flag][counter]=str;}
}
//Everything works fine here and I receive all elements in myArray!

Mr J
07-14-2004, 03:23 PM
You are creating a new array every time funct1 is run and at that stage the array is empty. Declare the array globally

function func1(mystring)
{ myArray=new Array();

brakeInto(mystring,0);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum