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 to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to iterate over a set of systematically named variables

    I have a question about proper syntax in iterative functions. I want to compare one master array to a large set (180+) of other arrays. The large set of arrays to be compared are systematically named (scorespec1, scorespec2, scorespec3...). Each comparison will be made not one-to-one but through an algorithm and then have the results stored in another set of arrays that are also systematically named for later query. I not worried about getting the algorithm right just yet. I'm just not sure of the proper syntax to iterate through my arrays. For instance, this is but one of the syntax structures I have tried but failed to get working:

    Code:
    for (i=01;i=186;i++){
      if (scorespec+(i)[04]=unknownspec[16]){
        resultarray+(i)[01]=true;}
      else{
        resultarray+(i)[01]=false;}}
    My main problem here is I don't know how to structure the syntax to include the counter variable in my for-loop in the variable name. I've tried a variety of different syntaxes in addition to what I show above and it just doesn't seem to work right. What syntax should I be using?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by WyoBuckeye View Post
    Code:
    for (i=01;i=186;i++){
      if (scorespec+(i)[04]=unknownspec[16]){
        resultarray+(i)[01]=true;}
      else{
        resultarray+(i)[01]=false;}}
    My main problem here is I don't know how to structure the syntax to include the counter variable in my for-loop in the variable name. I've tried a variety of different syntaxes in addition to what I show above and it just doesn't seem to work right. What syntax should I be using?
    Numbers should not have a zero prefix - if they do they may be interpreted as octal. The index of an array must be a decimal number.

    if (scorespec+(i)[04]=unknownspec[16]){

    = is assignment. == is comparison.


    "Success is the ability to go from one failure to another with no loss of enthusiasm." - Sir Winston Churchill, British politician (1874 - 1965)
    Last edited by Philip M; 12-30-2011 at 04:37 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    WyoBuckeye (12-30-2011)

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Numbers should not have a zero prefix - if they do they may be interpreted as octal. The index of an array must be a decimal number.

    if (scorespec+(i)[04]=unknownspec[16]){

    = is assignment. == is comparison.
    Made the adjustments that you recommended, but I am still having trouble with getting the syntax correct. Maybe what I am trying to do is not a good strategy for Javascript. Any other suggestions would be appreciated. Here is what I just tried as an experiment:

    Code:
    function score(){
    	for (i=1;i=10;i++){
    		if (species+(i)[11]==02){
    			alert("it worked")}
    		else{
    			alert(species1[11])
    			break}}};
    I tried some other variants too, but each time I get the alert "2" which tells me I can access the array directly, but not using the counter variable from the for-loop.

    Note: That code is greatly simplified from what I need, but when I am using unfamiliar methods I start out simple, get that to work and then build upon it. In this case, I cannot even get the simplest method to work.
    Last edited by WyoBuckeye; 12-30-2011 at 07:28 PM.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    <script>
    scorespec0 = [0,1,2,3,4]
    scorespec1 = [5,6,6,8,9]
    
    function displayArrayValues(){
     for(var i=0;i<2;i++){
      var a = window["scorespec"+i];
      for(var j=0;j<a.length;j++){document.write(a[j] + "<br>")}
     }
    
    }
    </script>
    </head>
    <body>
    <script>
    displayArrayValues();
    </script>
    </body>
    </html>

  • Users who have thanked DaveyErwin for this post:

    WyoBuckeye (12-30-2011)

  • #5
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Using the window object worked. I needed to rework a couple things to make it fit my needs, but using the window object was the way to go. Thanks.


  •  

    Posting Permissions

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