Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem with local/global arrays

    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!

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •