| Author |  | 
      
        | patrickm Senior Member
 
  
 
 Joined: February 22 2007
 Location: United States
 Online Status: Offline
 Posts: 188
 | 
          I am trying to write some macros that use case statements
           | Posted: July 25 2013 at 10:31 | IP Logged |   |  
           | 
 |  to read and write some variables.  If I hard code the
 variable name it works fine.
 
 ph_setglobal_s("SB_STATE_1", case("{SB_MODE_1}" when
 "play" then"2" when "pause" then "3" when "stop" then
 "0"))
 
 What I would like to do is access the variables in a
 loop counter.
 
 ph_setglobal_s("SB_STATE_" + "[LOCAL9]", case("SB_MODE_"
 + "[LOCAL9]"
 when "play" then"2" when "pause" then "3" when "stop"
 then "0"))
 
 That returns a zero but the variable doesn't change.
 
 What is the correct way of doing this?
 
 Thanks
 Patrick
 
 
 Edited by patrickm - July 25 2013 at 10:34
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | dhoward Admin Group
 
  
  
 Joined: June 29 2001
 Location: United States
 Online Status: Offline
 Posts: 4447
 | 
          Patrick,
           | Posted: July 25 2013 at 11:49 | IP Logged |   |  
           | 
 |  
 You can simplify the code a little bit by using this:
 
 ph_setglobal_s("SB_STATE_[LOCAL9]", case("SB_MODE_[LOCAL9]"
 when "play" then"2" when "pause" then "3" when "stop"
 then "0"))
 
 If [LOCAL9] contains a value of 3, then after the substitution, our statement to be evaluated will look like this:
 
 ph_setglobal_s("SB_STATE_3", case("SB_MODE_3"
 when "play" then"2" when "pause" then "3" when "stop"
 then "0"))
 
 Herein lies actual problem.  The case statement is now switching on the string value "SB_MODE_3" and not what is actually contained in the global variable SB_MODE_3.  You could try to use variable substitution to handle this (like in your first statement) by using something like this:
 
 case("{SB_MODE_[LOCAL9]}" when...
 
 but it wont work and that kind of substitution gets ugly.
 
 To fix it, use this:
 
 ph_setglobal_s("SB_STATE_[LOCAL9]", case(ph_getglobal_s("SB_MODE_[LOCAL9]")
 when "play" then"2" when "pause" then "3" when "stop"
 then "0"))
 
 I tested this and it works.
 
 Hope this helps,
 
 Dave.
 
 | 
       
        | Back to Top |       | 
       
       
        |  | 
        | patrickm Senior Member
 
  
 
 Joined: February 22 2007
 Location: United States
 Online Status: Offline
 Posts: 188
 | 
          Dave,
           | Posted: July 25 2013 at 13:27 | IP Logged |   |  
           | 
 |  Thanks for the explanation.
 
 Patrick
 | 
       
        | Back to Top |     | 
       
       
        |  |