Announcement

Collapse
No announcement yet.

Use of eval() on LHS of if...else statement

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

  • Use of eval() on LHS of if...else statement

    There seems to be some restriction on using the eval() function on the left hand side of an if…else conditional statement. Is anyone aware of a work around so that essentially the following loop could be programmed?

    for (var i=0; i<=10; i++){
    if(eval("myVar1"+i) >= eval("myVar2"+i)){
    whatever}
    }

    Thanks for any feedback.
    Mike

  • #2
    Re: Use of eval() on LHS of if...else statement

    Mike
    Seems to be working fine
    Alex

    PHP Code:
    if(isLastBarOnChart()){
            var 
    nClose "close";
            var 
    nOpen  "open";
            var 
    Count 0;
            for (var 
    i=0i<=10i++){
                if(eval(
    nClose)(-i) > eval(nOpen)(-i)){
                    
    setBar(Bar.BgColor,-i,0,Color.yellow)
                    
    Count++;//should return number equal to bars with yellow background
                

            }
            
    debugPrintln(Count)
        } 

    Originally posted by mikejhelms
    There seems to be some restriction on using the eval() function on the left hand side of an if…else conditional statement. Is anyone aware of a work around so that essentially the following loop could be programmed?

    for (var i=0; i<=10; i++){
    if(eval("myVar1"+i) >= eval("myVar2"+i)){
    whatever}
    }

    Thanks for any feedback.
    Mike

    Comment


    • #3
      Dad, will I ever be as smart as Alex?
      Yes, son. You just have to keep working on it.

      Comment


      • #4
        But then again … How about restrictions on the left hand side of the “else” of an “if…else” statement. Because both lines 3 and 5 of the code below generate the syntax error: “Invalid assignment left-hand side” with the hat “^” under the equal signs of both lines. Any feedback is appreciated.

        Mike

        1. for(var i=1; i<=10; i++){
        2. if(eval("MyVar1"+i) >= eval("MyVar2"+i)){
        3. eval("vColor"+i) = Color.green;
        4. }
        5. else{eval("vColor"+i) = Color.red}
        6. }

        Comment


        • #5
          Mike
          I don't think that the left hand assignement error message is necessarily related to the use of eval().
          Try the following and see if it resolves the issue
          PHP Code:
          for(var i=1i<=10i++){
              if(eval(
          "MyVar1"+i) >= eval("MyVar2"+i)){
                  var 
          nColor = eval("vColor"+i);
                  
          nColor Color.green;
              } else {
                  var 
          nColor = eval("vColor"+i);
                  
          nColor Color.red
              
          }   

          If it does not resolve the issue you may need to post a complete code example that illustrates what you are trying to accomplish
          Alex


          Originally posted by mikejhelms
          But then again … How about restrictions on the left hand side of the “else” of an “if…else” statement. Because both lines 3 and 5 of the code below generate the syntax error: “Invalid assignment left-hand side” with the hat “^” under the equal signs of both lines. Any feedback is appreciated.

          Mike

          1. for(var i=1; i<=10; i++){
          2. if(eval("MyVar1"+i) >= eval("MyVar2"+i)){
          3. eval("vColor"+i) = Color.green;
          4. }
          5. else{eval("vColor"+i) = Color.red}
          6. }

          Comment


          • #6
            Yes it did work, Alex. Thank you. In terms of the number of variables involved it was also more efficient than my approach which was to define multiple color variables and use them in multiple draw statements. Your approach required only one color variable and one draw statement, both of which I enclosed within the same loop. Unfortunately after incorporating the loop into the study, the Performance Monitor informed me the average processing time for the study more than doubled. I’ve occasionally found this in the past and it’s always disappointed me. There’s a simplistic beauty to a well constructed loop. I see them as a minor victory of thought over mindless repetition. But with the unprecedented volume spikes we’ve seen recently, e.g at 12:59 today the emini had a one minute volume of over 94,000, I conserve CPU like a Bedouin conserves water. Thanks for the help, though. I appreciate it.

            Mike

            Comment


            • #7
              Mike
              You are most welcome
              Alex


              Originally posted by mikejhelms
              Yes it did work, Alex. Thank you. In terms of the number of variables involved it was also more efficient than my approach which was to define multiple color variables and use them in multiple draw statements. Your approach required only one color variable and one draw statement, both of which I enclosed within the same loop. Unfortunately after incorporating the loop into the study, the Performance Monitor informed me the average processing time for the study more than doubled. I’ve occasionally found this in the past and it’s always disappointed me. There’s a simplistic beauty to a well constructed loop. I see them as a minor victory of thought over mindless repetition. But with the unprecedented volume spikes we’ve seen recently, e.g at 12:59 today the emini had a one minute volume of over 94,000, I conserve CPU like a Bedouin conserves water. Thanks for the help, though. I appreciate it.

              Mike

              Comment

              Working...
              X