...

View Full Version : How to iterate over a set of systematically named variables



WyoBuckeye
12-30-2011, 04:06 PM
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:


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?

Philip M
12-30-2011, 04:31 PM
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)

WyoBuckeye
12-30-2011, 07:02 PM
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:


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.

DaveyErwin
12-30-2011, 07:48 PM
<!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>

WyoBuckeye
12-30-2011, 08:19 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum