Jump to content
Sign in to follow this  
iwato

Changing A Variable's Name Iteratively

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

Share this post


Link to post
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;}

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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" + "';");

Edited by Ingolme

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...