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 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    initiating a variable number of arrays

    Hi.

    I am trying to initiate a variable number of arrays. I recently discovered the eval() function thanks to VortexCortex (althought he stated that he did not recommend using it).


    Well I currently have the code to initiate my arrays such that...

    Code:
    var myArray0 = new Array(); 
    var myArray1 = new Array();
    var myArray2 = new Array();
    var myArray3 = new Array();
    As you can see, the number of arrays is predefined within the code however I am trying to initiate loop to initiate a variable number of arrays based on user input. Since my array names contain incrementing numbers on the ends of their names I thought to use the eval function to add the correct number onto the names like so...

    Code:
    var userInput = prompt("How many arrays would you like?");
    parseInt(userInput);
    for(i=0; i<=userInput; i++){
          eval('var myArray' + i + ' = new Array()');
    }
    Is there something wrong with what I am doing with the eval function?

  • #2
    New Coder
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hehe never mind. I realised that the whole line must be inside the eval() function.

  • #3
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Minor thing but the user will get one more array than they asked for with that loop. To get the number they entered you'd need:

    for (i=0; i < userInput; i++) {

    or

    for (i = 1; i <= userInput; i++) {

    Might not matter to your code but I thought I'd point it out anyway.

    david_kw

  • #4
    New Coder
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for pointing that out although I have already accounted for it

  • #5
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    there is an easy solution to it.

    instead of trying to create a variable amout of variables....
    create an array whose value are arrays them self.

    MyArray=new Array();
    i='0';
    ITslength= // im asumming you want to create the arrays depending on a specified number, if so grab that number and put it here
    while(i<ITslength){
    myArray[i][0]=//first value of array
    myArray[i][1]=//second value of array
    myArray[i][2]=//third value of array
    // and so on depending on how many values you want in your sub arrays
    }



    the to access them just call to the main array
    the first sub array would be MyArray[0]
    the second one would be MyArray[1]
    .... and so on


    hope this is what you are looking for in a way.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.


  •  

    Posting Permissions

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