Announcement

Collapse
No announcement yet.

Referencing sequentially numbered variables on left hand side of a statement

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Referencing sequentially numbered variables on left hand side of a statement

    Hi.Does anyone know if eval() can be combined in some way with .toString() to recognize sequentially named variables for the purpose of assigning them new values? For example for variables Var1, Var2,...Var10 which already have values, given that a condition within a loop is met can eval() and .toString() be used on the left hand side of a statement to simply specify one of the ten variables, but not their value which would be the case if only eval() was used, so that those variables can then be assigned a new value. I realize the following is not correct but conceptually something along the lines of:

    for(i=1; i < 11; i++) {
    if(XXX > YYY){
    eval("Var"”+i).toString() = i +5;

    or maybe


    ("Var"”+i).toString() = i + 5;
    }
    }

    If eval() and .toString() can not be used together in this way, is anyone aware of a work around that would accomplish the same thing? Thanks.

  • #2
    See if this does what you're asking for:
    PHP Code:
    debugClear();

    var 
    Var0=0;
    var 
    Var1=1;
    var 
    Var2=2;

    function 
    main() 
    {     
        for(
    i=111i++) {
             if(
    1){
                eval(
    "Var" " = " "+ 5");//convert the code line to text except for variables and include all of it inside the eval()
                
    debugPrintln("13: "+eval("Var"+i));
                return;    
            }
        }

    Last edited by waynecd; 07-08-2014, 03:58 AM.

    Comment


    • #3
      mikejhelms
      Perhaps I am missing something here. “MyVar”+i is already a string to begin with [just try debugPrintln(typeof (“MyVar”+i))] so why evaluate it only to convert it back to a string?
      Anyhow you do not need to use the toString() method to do that. Just concatenate it to an empty string eg eval(“MyVar”+i)+””
      This is basic JavaScript so you should be able to find most of this kind of information in the Core JavaScript Guide in the EFS KnowledgeBase or doing a search on the web
      Alex


      Originally posted by mikejhelms View Post
      Hi.Does anyone know if eval() can be combined in some way with .toString() to recognize sequentially named variables for the purpose of assigning them new values? For example for variables Var1, Var2,...Var10 which already have values, given that a condition within a loop is met can eval() and .toString() be used on the left hand side of a statement to simply specify one of the ten variables, but not their value which would be the case if only eval() was used, so that those variables can then be assigned a new value. I realize the following is not correct but conceptually something along the lines of:

      for(i=1; i < 11; i++) {
      if(XXX > YYY){
      eval("Var"”+i).toString() = i +5;

      or maybe


      ("Var"”+i).toString() = i + 5;
      }
      }

      If eval() and .toString() can not be used together in this way, is anyone aware of a work around that would accomplish the same thing? Thanks.

      Comment


      • #4
        ThanksWaynecd. I'll give your suggested approach a try.

        Comment


        • #5
          Alexthanks for your feedback and suggested references.

          Comment

          Working...
          X