Jump to content

Changing A Variable's Name Iteratively


iwato

Recommended Posts

QUESTION: How does one change the name of a variable iteratively?BACKGROUND: During an iteration I extract information from a string based on a matched pattern. This pattern repeats itself in the same string a different number of times depending on the string that is matched. With each occurrence of the pattern in the same string is associated new information. My goal is to capture this information and assign it to a uniquely identified variable. Because the number of times that the pattern repeats itself is neither known, nor fixed, the number of uniquely identified variables for each string is also unknown. I would like to produce the following sort of output:DESIRED ITERATIVE OUTPUT:varName1 = "1st string value";varName2 = "2nd string value";...varNameN = "nth string value";I have seen this done with movie clips in ActionScript, but can no longer find the reference. I remember the technique as being simple, but not particularly intuitive. Does someone know?Roddy

Link to comment
Share on other sites

While you can get exactly what you want with eval(), I don't recommend it.Why not use arrays instead?

varName = new Array();for (i = 0; i < 10; i++) {  varName[i] = "Value " + i;}

Link to comment
Share on other sites

While you can get exactly what you want with eval(), I don't recommend it.Why not use arrays instead?
varName = new Array();for (i = 0; i < 10; i++) {  varName[i] = "Value " + i;}

Sorry for getting back to you so late, but your suggestion would have required an additional iteration that I was able to avoid with a different approach. Moreover, once the variable name were created I was not at all sure that the following would work:
var varName[i] = "ith string of information";

Your comment in regard to the above code would surely be appreciated.Roddy

Link to comment
Share on other sites

Hmm, since I don't yet know exactly what you are trying to achieve, I can't be sure how to solve it for you.with the eval() function, you run a string as a piece of Javascript code, but it really slows down the program by a lot.Here's how it works:

var x = "hello";eval(x + " = 5");alert(hello); // the message box should display "5"

You can also use it like this:

var myArray = [  "First array element",  "Second array element",  "Third array element",];var x = 0;while (x < myArray.length) {  eval("varName" + x + " = '" + myArray[x] + "';");  x++}alert(varName1); // returns "Second array element" in an alert box

Link to comment
Share on other sites

Hmm, since I don't yet know exactly what you are trying to achieve, I can't be sure how to solve it for you.It's OK. As I said, I have found another way. I was hoping that you would comment on the following. Perhaps the absence of comment was your way of telling me that it cannot be done.var varName = "the ith string of information";where varName is an array whose ith element becomes the name of a new variable whose value is set to "the ith string of information".Roddy
Link to comment
Share on other sites

Hmm, since I don't yet know exactly what you are trying to achieve, I can't be sure how to solve it for you.
It's OK. As I said, I have found another way. I was hoping that you would comment on the following. Perhaps the absence of comment was your way of telling me that it cannot be done.var varName = "the ith string of information";where varName is an array whose ith element becomes the name of a new variable whose value is set to "the ith string of information".Roddy
If you just want to copy an array to another variable, you can do this:
myArr = new Array();myArr[0] = "string 1";myArr[1] = "Another string";myArr[2] = "String 3";myArr2 = new Array();for (i in myArr) {  myArr2[i] = myArr[i];}

Any changes in myArr2 will not appear in myArr.If you want another variable pointing to the array, you can do the following:

myArr2 = myArr;

If you change anything in myArr2, the change will appear in myArr as well.EDITI'm not sure if I completely understood what you mean. Here's possibly a better interpretation of what you were asking.You could do things this way:

eval("var " + varName[i] + " = '" + "the ith string of information" + "';");

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...