Using For To Change Variable Name


KenMandile

Recommended Posts

I am trying to save myself some time by using a for statement to step through a set of variable names.  I have 29 machines being monitored and when I need to change or create script for all 29 it takes awhile.

Here is an example of a piece of script that works fine:

 

while(1)
       
      Global Star_22_24hr = 300 * (sum(GREEN_22[systime(), systime()-86400] == 0))/86400  //calculates % of up time over past 24 hours
      Global Star_22_1hr = 300 * (sum(GREEN_22[systime(), systime()-3600] == 0))/3600     //calculates % of up time over past hour
      Delay(5)
 
endwhile
   
Star_22_24hr and Star_22_1hr are the variables that I am calculating using data from the corresponding channel GREEN_22.  
 
Rather than write this over 29 times, I tried this:

while(1)
       Private.machine = 17                      //machine #17 is the first machine in this group
       for (machine = 17, 28, machine ++)               //loop from 17 to 28
             Global "Star_" + machine + "_24hr" = 300 * (sum("GREEN_" + machine [systime(), systime()-86400] == 0))/86400             //calculates % of up time over past 24 hours
             Global "Star_"  + machine + "_1hr" = 300 * (sum("GREEN_" + machine [systime(), systime()-3600] == 0))/3600                  //calculates % of up time over past hour
       endfor
       delay(5)
endwhile
 
It runs without errors, but it also does not seem to be putting any values in the variables.
 
 
 
Link to comment
Share on other sites

You need to build up a string and pass that string to execute().  I'm not sure what the compiler is going to do with the code you wrote.  Anyhow, it should look like this:

 

execute("Global Star_" + machine + "_1hr = 300 * (sum(GREEN_" + machine + "[systime(), systime()-3600] == 0))/3600")

Link to comment
Share on other sites

Archived

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